JSON解析[和]

时间:2016-05-31 21:55:43

标签: .net json vb.net

我有来自URL的JSON数据,我需要拆分值,

"name":"michael",
"number":"0015454545",
"age":"22",
"interest":[
    ["swim","baseball","music","tenis"],
    ["washington","3","27","a"],
    ["New york","2","16","b"],
    ["los angeles","21","11","c"],
    ["dallas","7","22","a"]
]

我的代码:

Dim json As String = MyjsonDataLink
Dim ser As JObject = JObject.Parse(json)
Dim data As List(Of JToken) = ser.Children().ToList

For Each item As JProperty In data
    item.CreateReader()
    If item.Name = "interest" Then
        Response.Write(item.Value.ToString)
        Response.Write("<br><br><br>")
    Next
    End If
Next

但是,文本输出是,

["washington","3","27","a"],
["new york","2","16","b"],
["los angeles","21","11","c"],
["dallas","7","22","a"]]

如何提取&#34;华盛顿&#34;,&#34;纽约&#34;,&#34;洛杉矶&#34;和&#34;达拉斯&#34;字符串?

1 个答案:

答案 0 :(得分:0)

LINQ是为这种情况而构建的。这是一个控制台应用程序,说明:

Imports Newtonsoft.Json.Linq
Module Module1
  Sub Main()
    'ignore the <a> tag, it's just a trick for multi-line strings
    Dim json = <a>
{"name":"michael",
"number":"0015454545",
"age":"22",
"interest":[
    ["swim","baseball","music","tenis"],
    ["washington","3","27","a"],
    ["New york","2","16","b"],
    ["los angeles","21","11","c"],
    ["dallas","7","22","a"]
]}</a>.Value
    Dim ser As JObject = JObject.Parse(json)
    'now let LINQ do the work for us
    Dim output = From s In ser("interest").Children Select s.First
    For Each item In output
      Console.Write(item)
      Console.Write("<br/><br/><br/>")
    Next
    Console.ReadLine()
  End Sub
End Module

结果:

swim<br/><br/><br/>washington<br/><br/><br/>New york<br/><br/><br/>los angeles<br/><br/><br/>dallas<br/><br/><br/>