将 JSON 数组转换为 VB.NET 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14085546/
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
Convert JSON array to VB.NET array
提问by Radcom
I have a very simple JSON array and I would like to convert it to a VB.NET array. The JSON is:
我有一个非常简单的 JSON 数组,我想将它转换为 VB.NET 数组。JSON 是:
{"UPTIME": "UNKNOWN", "VERSION ": 2.0, "SENDTIME": "03:33:52", "SENDDATE": "28- 12-2012", "HOSTNAME": "My-PC"}
In this case [0]="UNKNOWN", [1]=2.0, [2]=03:33:52, etc.
在这种情况下[0]="UNKNOWN",[1]=2.0、[2]=03:33:52、 等。
I've read through previous questions and I'm just getting more confused and they all seem to be for more complex examples of json than I have. I am using the Newtonsoft.Json library as that seems to be recommended a lot, but I don't understand how it works.
我已经通读了以前的问题,但我越来越困惑,它们似乎都比我更复杂的 json 示例。我正在使用 Newtonsoft.Json 库,因为它似乎被推荐了很多,但我不明白它是如何工作的。
回答by
You can use Json.Net a .Net JSON Converter library. From what I see you should be able to convert the string to a Json object, then you can write a cast method to build an array from the members of that JSON object.
您可以使用 Json.Net 一个 .Net JSON 转换器库。从我看来,您应该能够将字符串转换为 Json 对象,然后您可以编写一个强制转换方法来从该 JSON 对象的成员构建一个数组。
回答by Radcom
I got it working in the end with random experimentation this produces a dictionary with the values in, which is close enough so I can create the variables I wanted without having to use a VB.NET array I thought I would have to use I hope this helps others:
我最终通过随机实验得到了它,这会生成一个包含值的字典,它足够接近,因此我可以创建我想要的变量而无需使用 VB.NET 数组我认为我必须使用我希望这个帮助他人:
Imports:
进口:
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Declared variables:
声明的变量:
Dim json As String 'this is the string of json data that needs decoding
Dim version As String
Dim obj As New JObject
Conversion code:
转换代码:
json = '{"UPTIME": "UNKNOWN", "VERSION ": 2.0, "SENDTIME": "03:33:52", "SENDDATE": "28- 12-2012", "HOSTNAME": "My-PC"}'
Dim deserializedProduct As Object = JsonConvert.DeserializeObject(Of Object)(json)
obj = JsonConvert.DeserializeObject(recv)
uptime = obj.Item("UPTIME").ToString
version = obj.Item("VERSION").ToString
Console.WriteLine(uptime)
Console.WriteLine(version)
Result:
结果:
UNKNOWN
2
回答by the_lotus
I haven't used any of those so I'm not aware of the usefuleness of it. There's a Json serializer class built-in .net System.Runtime.Serialization.Json
我没有使用过这些,所以我不知道它的用处。.net System.Runtime.Serialization.Json内置了一个 Json 序列化器类

