The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
Deserialize Json with Vb.Net RRS feed

  • Question

  • How can I deserialize a Json String by using VB.Net?


    Thursday, April 11, 2013 8:02 PM

Answers

  • That would be  :

    You need to do use :

    Imports Newtonsoft.Json

    JsonConvert.DeserializeObject(Of <The Class object>)(<JSON String>)

    For Example if your JSON string is

    [
    	{
    	color: "red",
    	value: "#f00"
    	},
    	{
    	color: "green",
    	value: "#0f0"
     } .....
    ]


    You need to create a class (lets say "MyColor")

    Public Class MyColor
    	Public Property color() As String
    		Get
    			Return m_color
    		End Get
    		Set
    			m_color = Value
    		End Set
    	End Property
    	Private m_color As String
    	Public Property value() As String
    		Get
    			Return m_value
    		End Get
    		Set
    			m_value = Value
    		End Set
    	End Property
    	Private m_value As String
    End Class
    

    Then your deserialization code will be :

    JsonConvert.DeserializeObject(Of MyColor)(<JSON String>)
    Hope this helps.

    - Girija


    • Edited by Girija Beuria Thursday, April 11, 2013 8:27 PM
    • Marked as answer by PhilSpe Friday, April 12, 2013 11:23 PM
    Thursday, April 11, 2013 8:21 PM
  • Hello,

    Make sure you have the latest version of NuGet (2.2) installed. It's only from 2.1 onwards that Portable Class Libraries and Windows Phone 8 are supported.

    After updating NuGet, close and reopen Visual Studio and try to add the package again, it should install just fine now.


    Kenneth Truyers www.kenneth-truyers.net

    • Marked as answer by PhilSpe Friday, April 12, 2013 11:23 PM
    Friday, April 12, 2013 10:57 PM

All replies

  • That would be  :

    You need to do use :

    Imports Newtonsoft.Json

    JsonConvert.DeserializeObject(Of <The Class object>)(<JSON String>)

    For Example if your JSON string is

    [
    	{
    	color: "red",
    	value: "#f00"
    	},
    	{
    	color: "green",
    	value: "#0f0"
     } .....
    ]


    You need to create a class (lets say "MyColor")

    Public Class MyColor
    	Public Property color() As String
    		Get
    			Return m_color
    		End Get
    		Set
    			m_color = Value
    		End Set
    	End Property
    	Private m_color As String
    	Public Property value() As String
    		Get
    			Return m_value
    		End Get
    		Set
    			m_value = Value
    		End Set
    	End Property
    	Private m_value As String
    End Class
    

    Then your deserialization code will be :

    JsonConvert.DeserializeObject(Of MyColor)(<JSON String>)
    Hope this helps.

    - Girija


    • Edited by Girija Beuria Thursday, April 11, 2013 8:27 PM
    • Marked as answer by PhilSpe Friday, April 12, 2013 11:23 PM
    Thursday, April 11, 2013 8:21 PM
  • Hi thanks for your anwser!

    Is there a way without json.net? I can't import it to my project? It says it's not compatible to my framework...

    Friday, April 12, 2013 7:48 PM
  • You need to install the JSON.NET from nugget :

    Install from Nugget Packages (Project right click --> Manage Nugget ...)

    or install using Package Manager Console.

    Install-Package NewtonSoft.Json

    - Girija

    Friday, April 12, 2013 8:44 PM
  • Yea but i get this error message:


    Friday, April 12, 2013 9:15 PM
  • Hi,

    I just installed on a new VB phone app and it works perfectly. My Phone target is 8.0 and Vs is Ultimate though.

    Are you able to create a new project and install the same or is it the same error.

    Do you have any previous installation. If so , please remove and re-install.

    - Girija

    Friday, April 12, 2013 9:41 PM
  • Have you seen the new features of VS 2012.2 around JSON? All the documentation talks about it being a web only feature, but it is working for me in a full VS install. This means that as long as you have the web edition installed (and maybe not even then), you should be able to paste JSON as a class. Then, converting a JSON result to a class should be pretty straightforward.

    http://blogs.msdn.com/b/webdev/archive/2012/12/18/paste-json-as-classes-in-asp-net-and-web-tools-2012-2-rc.aspx
    Friday, April 12, 2013 10:09 PM
  • Hello,

    Make sure you have the latest version of NuGet (2.2) installed. It's only from 2.1 onwards that Portable Class Libraries and Windows Phone 8 are supported.

    After updating NuGet, close and reopen Visual Studio and try to add the package again, it should install just fine now.


    Kenneth Truyers www.kenneth-truyers.net

    • Marked as answer by PhilSpe Friday, April 12, 2013 11:23 PM
    Friday, April 12, 2013 10:57 PM
  • Thanks to all!

    Kenneth was right. It was the NuGet Version. Thanks!

    Friday, April 12, 2013 11:24 PM