Solved

parse json string into model

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calling web service and its methods dynamically 2 43
VB.Net How to Exit Sub - Exit Form??? 5 51
Error in JQuery 5 39
Visual Studio 2013 and Eclipse Neon 6 13
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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

23 Experts available now in Live!

Get 1:1 Help Now