vb.net 在 Visual Basic 中使用 json.net 解析嵌套的 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13416497/
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
Parse nested JSON with json.net in Visual Basic
提问by user1829564
I have nested JSON strings that i would like to parse out the appropriate values from much like below. As i am learning by doing I am struggling a little bit, I have the first part working in that I can parse out single JSON strings, and return the appropriate value using code example 1 below, however i am stuck with a JSON string that is problematic in that it is nested, so the same approach won't work
我有嵌套的 JSON 字符串,我想从下面的很像中解析出适当的值。当我边做边学时我有点挣扎,我有第一部分工作,我可以解析单个 JSON 字符串,并使用下面的代码示例 1 返回适当的值,但是我被困在一个 JSON 字符串中问题在于它是嵌套的,所以同样的方法不起作用
{
"jsonrpc":"2.0",
"method":"Player.OnPause",
"params":{
"data": { "item": { "id":29, "type":"episode" },
"player": { "playerid":1, "speed":0 }
},
"sender":"xbmc"
}
}
And the code...
还有代码...
Dim JSON As String
Dim values As Newtonsoft.Json.Linq.JObject
JSON = JSON STRING WOULD GO HERE, COMES from TCP IP STREAM
values = JObject.Parse(JSON)
Console.WriteLine(values.GetValue("method"))
Using that example i can extract the method key (e.g. Player.OnPause) from the first level JSON string, but how can i extract data from the second, and third level strings, For example in the above string, being able to get to Data level JSON values, and Item level JSON values. Is this possible in a similar way to the above?
使用该示例,我可以从第一级 JSON 字符串中提取方法键(例如 Player.OnPause),但是如何从第二级和第三级字符串中提取数据,例如在上面的字符串中,能够获取数据级别 JSON 值和项目级别 JSON 值。这可能以与上述类似的方式进行吗?
Appreciate any tips you could provide, I am a learn by examples person, but just struggling to apply something to read multiple nested JSON strings, or multiple levels. No doubt it will be an easy thing that i am missing, but id appreciate any help someone could provide.
感谢您提供的任何提示,我是一个通过示例学习的人,但只是努力应用某些东西来读取多个嵌套的 JSON 字符串或多个级别。毫无疑问,我错过了这将是一件容易的事情,但我很感激有人可以提供的任何帮助。
Thanks
谢谢
回答by igrimpe
Dim jsonstring = IO.File.ReadAllText("json.txt")
Dim jo = Json.Linq.JObject.Parse(jsonstring)
Dim playerid = jo("params")("data")("player")("playerid")
Do you mean something like this? "json.txt" simply contains your JSON string.
你的意思是这样的吗?“json.txt”只包含您的 JSON 字符串。
回答by Simon Barnett
After searching here and else where for an polymorhic example of parsing javascrip object notation into a .net dictionary I gave up and wrote my own. Hope someone finds this useful ;)
在here和其他地方搜索将javascrip对象表示法解析为.net字典的多态示例后,我放弃并编写了自己的字典。希望有人觉得这很有用 ;)
Public Class jsonDictionary
Inherits Dictionary(Of String, Object)
#Region "Public Properties"
Private _parent As jsonDictionary
Public ReadOnly Property Parent() As jsonDictionary
Get
Return _parent
End Get
End Property
#End Region
#Region "Initialisation and Finalisation"
Public Sub New(ByVal Parent As jsonDictionary, ByVal strJson As String)
_parent = Parent
For Each Element As String In SplitJSON(CharTrim(strJson))
Dim elName As String = CharTrim(Element.Split(":")(0).Trim)
Select Case Element.Split(":")(1).Trim.Substring(0, 1)
Case "{"
Me.Add(elName, New jsonDictionary(Me, Element.Substring(InStr(Element, ":"))))
Case "["
Dim ElArray As New List(Of jsonDictionary)
For Each ArrayElement As String In SplitJSON(CharTrim(Element.Substring(InStr(Element, ":"))))
ElArray.Add(New jsonDictionary(Me, ArrayElement))
Next
Me.Add(elName, ElArray)
Case Else
Me.Add(elName, Element.Split(":")(1).Trim)
End Select
Next
End Sub
#End Region
#Region "Private Methods"
Public Shared Function CharTrim(ByVal Str As String) As String
Return Str.Trim.Substring(1, Str.Length - 2)
End Function
Private Function SplitJSON(ByVal str As String) As String()
Dim ret() As String = Nothing
Dim sqCount As Integer = 0
Dim clCount As Integer = 0
Dim buildStr As New System.Text.StringBuilder
For i As Integer = 0 To str.Length - 1
Select Case str.Substring(i, 1)
Case ","
If sqCount = 0 And clCount = 0 Then
Try
ReDim Preserve ret(UBound(ret) + 1)
Catch ex As Exception
ReDim ret(0)
Finally
ret(UBound(ret)) = buildStr.ToString
buildStr = New System.Text.StringBuilder
End Try
Else
buildStr.Append(str.Substring(i, 1))
End If
Case Else
buildStr.Append(str.Substring(i, 1))
Select Case str.Substring(i, 1)
Case "["
sqCount += 1
Case "]"
sqCount -= 1
Case "{"
clCount += 1
Case "}"
clCount -= 1
End Select
End Select
Next
If buildStr.ToString.Length > 0 Then
Try
ReDim Preserve ret(UBound(ret) + 1)
Catch ex As Exception
ReDim ret(0)
Finally
ret(UBound(ret)) = buildStr.ToString
End Try
End If
Return ret
End Function
#End Region
End Class

