Solved

parse json string into model

Posted on 2013-12-05
3
469 Views
Last Modified: 2013-12-16
Hi!

This is my json data and JavaScriptSerializer() is new to me.

"products": [{
		"id": "27",
		"username": "user1",
		"recordfile": "file1.3gp",
		"status": "1"
	},
	{
		"id": "35",
		"username": "user2",
		"recordfile": "file2.3gp",
		"status": "1"
	},
	{
		"id": "45",
		"username": "user3",
		"recordfile": "file3.3gp",
		"status": "1"
	}],
	"success": 1

Open in new window


i'm using
Dim deserializedDictionary2 As Dictionary(Of String, Object) = deserializer.Deserialize(Of Dictionary(Of String, Object))(json)

and with this i get it into a dictionary containing arraylist.

i have 2 questions
1.how do i get the values from the arraylists
2. i would prefer to have the data in a model
like this
Class products
    Public id As String
    Public username As String
    Public recordfile As String
    Public status As String
End Class

Open in new window


what would be the best way to do that?
0
Comment
Question by:jamppi
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39700055
I recommend installing the JSON library from Newtonsoft, which you can acquire through NuGet. You'll also want to add in braces around this data since it's not technically Javascript object in its current state. Once you do that, you can do deserialize the JSON in this manner:

Imports Newtonsoft.Json

Module Module1

    Sub Main()
        Dim json As String = "{""products"": [{" & _
                             "        ""id"": ""27""," & _
                             "        ""username"": ""user1""," & _
                             "        ""recordfile"": ""file1.3gp""," & _
                             "        ""status"": ""1""" & _
                             "    }," & _
                             "    {" & _
                             "        ""id"": ""35""," & _
                             "        ""username"": ""user2""," & _
                             "        ""recordfile"": ""file2.3gp""," & _
                             "        ""status"": ""1""" & _
                             "    }," & _
                             "    {" & _
                             "        ""id"": ""45""," & _
                             "        ""username"": ""user3""," & _
                             "        ""recordfile"": ""file3.3gp""," & _
                             "        ""status"": ""1""" & _
                             "    }]," & _
                             "    ""success"": 1}"

        Dim products As JsonObject = JsonConvert.DeserializeObject(Of JsonObject)(json)
    End Sub

End Module

Class products
    Public id As String
    Public username As String
    Public recordfile As String
    Public status As String
End Class

Class JsonObject
    Public Products As List(Of products)
    Public Success As String
End Class

Open in new window


Note the additional braces in lines 6 and 24 to make the data a Javascript object. After the deserialization, you can access the products as:

For Each prod As product in products.Products
    Console.WriteLine(prod.id)
    Console.WriteLine(prod.username)
    Console.WriteLine(prod.recordfile)
    Console.WriteLine(prod.status)
Next

Open in new window

0
 

Author Comment

by:jamppi
ID: 39715204
well i prefer to use the native JavaScriptSerializer() in .net
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39715767
OK, so use the JavascriptSerializer. But the remainder of my previous comment still stands:

Imports System.Web.Script.Serialization

Module Module1

    Sub Main()
        Dim json As String = "{""products"": [{" & _
                             "        ""id"": ""27""," & _
                             "        ""username"": ""user1""," & _
                             "        ""recordfile"": ""file1.3gp""," & _
                             "        ""status"": ""1""" & _
                             "    }," & _
                             "    {" & _
                             "        ""id"": ""35""," & _
                             "        ""username"": ""user2""," & _
                             "        ""recordfile"": ""file2.3gp""," & _
                             "        ""status"": ""1""" & _
                             "    }," & _
                             "    {" & _
                             "        ""id"": ""45""," & _
                             "        ""username"": ""user3""," & _
                             "        ""recordfile"": ""file3.3gp""," & _
                             "        ""status"": ""1""" & _
                             "    }]," & _
                             "    ""success"": 1}"

        Dim serializer As New JavaScriptSerializer()
        Dim products As JsonObject = serializer.Deserialize(Of JsonObject)(json)
    End Sub

End Module

Class products
    Public id As String
    Public username As String
    Public recordfile As String
    Public status As String
End Class

Class JsonObject
    Public Products As List(Of products)
    Public Success As String
End Class

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now