Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

parse json string into model

Posted on 2013-12-05
3
Medium Priority
?
475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

670 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