VB.Net 将 JSON 数据转换为变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27137329/
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
VB.Net Convert JSON data to Variables
提问by Raymond
I am revisiting VB.NET after years of not doing anything with it. SO take it a little easy on me, also I have googled this and searched through Stackoverflow and have not found something to help.
在多年没有对它做任何事情之后,我正在重新审视 VB.NET。所以对我来说放轻松一点,我也用谷歌搜索了这个并通过 Stackoverflow 进行了搜索,但没有找到任何帮助。
I have this JSON coming in from a web API.
我有这个来自 Web API 的 JSON。
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Dim rawresp As String
rawresp = "[{"Var1":"data1","Var2":"data2","Var3":"data3","Var4":"data4"}]"
I have tried this way of doing it
我已经尝试过这种方式
Dim json As JObject = JObject.Parse(rawresp)
and I have tried this way
我已经尝试过这种方式
Dim obj = JsonConvert.DeserializeObject(Of Vars)(rawresp)
Public Class Vars
Public Property Var1 As String
Public Property Var2 As String
Public Property Var3 As String
Public Property Var4 As String
End Class
I am really having trouble seeing that deserializing json is this difficult, I must be missing something basic.
我真的很难看到反序列化 json 是如此困难,我一定是遗漏了一些基本的东西。
采纳答案by Shiva
Here's a working dotNetFiddle: https://dotnetfiddle.net/81Huv6
这是一个工作 dotNetFiddle:https://dotnetfiddle.net/81Huv6
Wait a few seconds for the code to run, and then look at the output in the Console Window (bottom pane on that page).
等待代码运行几秒钟,然后查看控制台窗口(该页面的底部窗格)中的输出。
Here's the output of the Deserialization.
这是反序列化的输出。


You have to deserialize the JSON into an Arrayof Vars. Like So.
你必须在反序列化JSON到Array的Vars。像这样。
Dim varses() = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Vars())(rawresp)
Notice also that the literal JSON string has the name and value strings enclosed in double quotes. You were probably getting invalid JSON format error due to the missing extra quote around each name and each value.
另请注意,文字 JSON 字符串具有用双引号括起来的名称和值字符串。由于每个名称和每个值周围缺少额外的引号,您可能会收到无效的 JSON 格式错误。
rawresp = "[{""Var1"":""data1"",""Var2"":""data2"",""Var3"":""data3"",""Var4"":""data4""}]"
Also be sure to check for empty array etc before attempting to access the items inside.
在尝试访问里面的项目之前,还要确保检查空数组等。
Here's the entire code listing that works.
这是有效的整个代码清单。
Imports System
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
' Author: Shiva Manjunath
' Author's Stackoverflow Profile: http://stackoverflow.com/users/325521/shiva
Public Module Module1
Public Sub Main()
Dim rawresp As String
rawresp = "[{""Var1"":""data1"",""Var2"":""data2"",""Var3"":""data3"",""Var4"":""data4""}]"
Console.WriteLine("Raw JSON : " + rawresp)
Console.WriteLine()
Console.WriteLine("---BEGIN JSON Deserialization")
Dim varses() = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Vars())(rawresp)
' Loop over each Var in the Array of Vars.
For Each oneVar As Vars In varses
' Avoid Nothing vars.
If oneVar IsNot Nothing Then
Console.WriteLine(" Var1 = " + oneVar.Var1)
Console.WriteLine(" Var2 = " + oneVar.Var2)
Console.WriteLine(" Var3 = " + oneVar.Var3)
Console.WriteLine(" Var4 = " + oneVar.Var4)
End If
Next
Console.WriteLine("---END JSON Deserialization")
End Sub
End Module
Public Class Vars
Public Property Var1 As String
Public Property Var2 As String
Public Property Var3 As String
Public Property Var4 As String
End Class
回答by Hassan Ali Salem
You are parsing array as object try to remove "[" and "]" from the json string you have to do it like this
您正在将数组解析为对象尝试从 json 字符串中删除“[”和“]”,您必须这样做
Try
Dim rawresp As String = "{'name':'hassan','age':24,'sex':'male'}"
Dim json As JObject = JObject.Parse(rawresp)
MsgBox(json.Item("name"))
Catch ex As Exception
MsgBox(ex.Message)
End Try
回答by Slippery Pete
The problem is that your JSON represents an array (of length 1) of dictionaries. To deserialize this in C# (sorry I am not very familiar with VB.Net):
问题是您的 JSON 表示一个字典数组(长度为 1)。在 C# 中反序列化它(对不起,我对 VB.Net 不是很熟悉):
var obj = JsonConvert.DeserializeObject<Dictionary<string, string>[]>(rawresp);
objwill be an array containing a single dictionary with the 4 name-value-pairs shown in the JSON.
obj将是一个包含单个字典的数组,其中包含 JSON 中显示的 4 个名称-值对。

