Solved

parse json string into model

Posted on 2013-12-05
3
459 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 74

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 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

19 Experts available now in Live!

Get 1:1 Help Now