How to Parse Json list in VB.NET Newtonsoft

10,138

As per as my knowledge there are multiple ways to do it, i prefer following way which is more easy ,straight and maintainable

there is JSON serializer and deserializer provided inbuilt in .net framework, requirement for that is, you have to create classes which will map to your JSON. you can have a look at or http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.deserialize.aspx

In the above case you have to create your class like below

class UserLatLang
{
public long lat { get;set;}
public long lng { get;set;}
public long user_id {get;set;}
public string user {get;set;} 
}

after this you can

var serializer = new JavaScriptSerializer();
var listofUserLatLang = serializer.Deserialize<UserLatLang>(responseText);

and you will get a list of UserLatLang in listofUserLatLang

or you can also refer class from http://msdn.microsoft.com/en-us/library/bb412179.aspx

Once you get list of UserLatLang you can directly bind it to DataGrid

Hope this solves your problem

thanks, Sandesh Daddi

Share:
10,138
milo2011
Author by

milo2011

Impossible is nothing!

Updated on June 04, 2022

Comments

  • milo2011
    milo2011 almost 2 years

    I receive from a webservice, the following JSON:

    [ {"lat": 42.41375, "user_id": 762, "user": "John", "lng": 23.02187},  {"lat": 42.46835, "user_id": 675, "user": "Mike", "lng": 23.02612},  {"lat": 42.85672, "user_id": 654, "user": "Jane", "lng": 23.01029},  {"lat": 42.46876, "user_id": 687, "user": "Luke", "lng": 23.02676} ]
    

    I want to add this information using VB.net, row by row, to a DataGridView.

    I'm new to JSON.net.

    How to loop through the whole list?

    How to go about parsing it?

  • milo2011
    milo2011 over 10 years
    Thanks, Sandesh! After reading more about JSON, I realized that the brackets '[' means that I am dealing with a Array. How can I loop throug the array?
  • Sandesh Daddi
    Sandesh Daddi over 10 years
    Did you try solution as var serializer = new JavaScriptSerializer(); var listofUserLatLang = serializer.Deserialize<UserLatLang>(responseText); then it will return you list, which you can directly loop through using simple foreach loop
  • milo2011
    milo2011 over 10 years
    Dim a As JArray = JArray.Parse(RichTextBox1.Text) For Each o As JObject In a.Children(Of JObject)() DataGridView1.Rows.Add(o("user_id"), o("user"), o("lat"), o("lng")) Next
  • Sandesh Daddi
    Sandesh Daddi over 10 years
    Sorry I missed VB.Net...anyways I think you solved it...congrats