如何在 VB.NET Newtonsoft 中解析 Json 列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18076744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:33:30  来源:igfitidea点击:

How to Parse Json list in VB.NET Newtonsoft

jsonvb.netparsingdatagridviewjson.net

提问by milo2011

I receive from a webservice, the following JSON:

我从网络服务收到以下 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.

我想使用 VB.net 将这些信息逐行添加到 DataGridView。

I'm new to JSON.net.

我是 JSON.net 的新手。

How to loop through the whole list?

如何遍历整个列表?

How to go about parsing it?

如何解析它?

回答by Sandesh Daddi

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

.net 框架中提供了内置的 JSON 序列化器和反序列化器,要求是,您必须创建将映射到您的 JSON 的类。你可以看看或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

您将在 listofUserLatLang 中获得 UserLatLang 列表

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

或者您也可以从http://msdn.microsoft.com/en-us/library/bb412179.aspx参考课程

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

获得 UserLatLang 列表后,您可以将其直接绑定到 DataGrid

Hope this solves your problem

希望这能解决您的问题

thanks, Sandesh Daddi

谢谢,桑德什·达迪