Link to home
Start Free TrialLog in
Avatar of jamppi
jamppiFlag for Sweden

asked on

parse json string into model

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?
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of jamppi

ASKER

well i prefer to use the native JavaScriptSerializer() in .net
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial