如何在VB.NET Newtonsoft中解析Json列表 [英] How to Parse Json list in VB.NET Newtonsoft
问题描述
我从Web服务收到以下JSON:
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} ]
我想使用VB.net逐行将这些信息添加到DataGridView.
I want to add this information using VB.net, row by row, to a DataGridView.
我是JSON.net的新手.
I'm new to JSON.net.
如何遍历整个列表?
如何进行解析?
推荐答案
据我所知,有多种方法可以做到这一点,我更喜欢以下更简单,更直接,更易于维护的方法
As per as my knowledge there are multiple ways to do it, i prefer following way which is more easy ,straight and maintainable
.net框架中内置了JSON序列化器和反序列化器,为此,您必须创建将映射到JSON的类.您可以查看或 http://msdn.microsoft .com/en-us/library/system.web.script.serialization.javascriptserializer.deserialize.aspx
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;}
}
之后,您可以
var serializer = new JavaScriptSerializer();
var listofUserLatLang = serializer.Deserialize<UserLatLang>(responseText);
,您将在listofUserLatLang中获得UserLatLang的列表
and you will get a list of UserLatLang in listofUserLatLang
,或者您也可以从 http://msdn.microsoft.com/zh-CN/library/bb412179.aspx
一旦获得UserLatLang的列表,就可以直接将其绑定到DataGrid
Once you get list of UserLatLang you can directly bind it to DataGrid
希望这可以解决您的问题
Hope this solves your problem
谢谢, 桑德什·达迪(Sandesh Daddi)
thanks, Sandesh Daddi
这篇关于如何在VB.NET Newtonsoft中解析Json列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!