如何在vb.net中解析json

时间:2014-03-25 07:29:11

标签: json vb.net parsing

我有json:{'success':'1','return':[{'id':'32928888','datetime':'2014-03-25 02:49:21','price':'0.02800939','quantity':'0.26094649','total':'0.00730895','io':'Buy'},{'id':'32928884','datetime':'2014-03-25 02:49:18','price':'0.02800939','quantity':'0.09930853','total':'0.00278157','io':'Buy'},{'id':'32928850','datetime':'2014-03-25 02:48:49','price':'0.02800939','quantity':'0.00093585','total':'0.00002621','io':'Buy'},{'id':'32928848','datetime':'2014-03-25 02:48:48','price':'0.02800939','quantity':'0.23547262','total':'0.00659544','io':'Sell'},{'id':'32928698','datetime':'2014-03-25 02:47:42','price':'0.02800939','quantity':'0.25553470','total':'0.00715737','io':'Sell'},{'id':'32928540','datetime':'2014-03-25 02:47:05','price':'0.02800940','quantity':'0.00820048','total':'0.00022969','io':'Sell'}]}

我使用代码:

Public Function parse_json(ByVal json As String) As Nullable
    Try
        Dim jResults As JObject = JObject.Parse(json)
        Dim results As List(Of JToken) = jResults.Children().ToList()

        For Each item As JProperty In results
            item.CreateReader()
            MsgBox(item.Value("id"))
            MsgBox(item.Value("datetime"))
        Next
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Function

但是我收到错误说:System.InvalidOperationException:无法访问Newtonsoft.Json.Linq.JValue上的子值。我究竟做错了什么?我需要获得所有ID,价格等。

1 个答案:

答案 0 :(得分:1)

return是完整对象的子项:

Dim results As JArray = jResults.GetValue("return");