How to Parse Json list in VB.NET Newtonsoft

十年热恋 提交于 2019-12-10 22:14:05

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/18076744/how-to-parse-json-list-in-vb-net-newtonsoft

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!