简体   繁体   English

使用方括号 VB.NET 反序列化 JSON

[英]Deserializing JSON with square brackets VB.NET

so I have the following json string:所以我有以下 json 字符串:

{
    "domain": "something.com",
    "subject": {
        "id": "1111",
        "name": "My name",
        "date": "2016-07-06"
    },
    "atributes": [
        {
            "height": "178",
            "age": "45"
        }
    ]
}

I can parse the first "round bracket" without a problem by doing this:通过这样做,我可以毫无问题地解析第一个“圆括号”:

    Dim json = Await client.GetStringAsync(url) //gettin the json from an API
    Dim jss = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Object)(json) 

    For Each Jproperty In jss("subject")

        Dim name1 = Jproperty.Name.ToString
        Dim value1 = Jproperty.Value.ToString

        TextBox1.Text &= name1 & ": " & value 1
        //Which returns: 
        //id: 111
        //name: My name
        //date: 2016-07-06

    Next

Cool, so far so good, but I can't for the life of me doing the same with "atributes", maybe because it's inside some square brackets which make it an array I believe?酷,到目前为止一切都很好,但我不能在我的生活中对“属性”做同样的事情,也许是因为它在一些方括号内,这使它成为我相信的数组? When I try to do the same in ANOTHER for each I got an exception that says that "Name" cannot be found in the 'JObject' object.当我尝试在另一个中为每个人做同样的事情时,我得到一个异常,说在“JObject”object 中找不到“名称”。 Using Dim height = Jproperty.Name.ToString使用Dim height = Jproperty.Name.ToString

BUT when I call "atributes" using something like但是当我使用类似的东西调用“属性”时

Dim height = Jproperty.ToString (without "Name") it works but returns a giant string and that's not really what I need. Dim height = Jproperty.ToString (without "Name")它可以工作但返回一个巨大的字符串,这并不是我真正需要的。

Thanks and I hope I made sense!谢谢,我希望我说得通!

If the JSON you're expecting from your API call is always of the same structure, I'd recommend creating a class and deserializing to that instead of Object . If the JSON you're expecting from your API call is always of the same structure, I'd recommend creating a class and deserializing to that instead of Object . (The content in square brackets will be represented by a collection type in your class.) (方括号中的内容将由 class 中的集合类型表示。)

Eg例如

Class JsonClass
    <JsonProperty("domain")>
    Property [Domain] As String
    <JsonProperty("subject")>
    Property [Subject] As cSubject
    <JsonProperty("atributes")>
    Property [Attibutes] As List(Of cAttribute)

    Class cSubject
        <JsonProperty("id")>
        Property [Id] As String
        <JsonProperty("name")>
        Property [Name] As String
        <JsonProperty("date")>
        Property [Date] As String
    End Class

    Class cAttribute
        <JsonProperty("height")>
        Property [Height] As String
        <JsonProperty("age")>
        Property [Age] As String
    End Class
End Class

Dim jss As JsonClass = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JsonClass)(json) 

There is no need for any looping through raw JSON using this approach.使用这种方法,无需通过原始 JSON 进行任何循环。

You are right, "square brackets" represents an Array of objects.你是对的,“方括号”代表一个对象数组。
Notice array of objects , this mean you need another "third" foreach to loop through properties of the object.注意对象数组,这意味着您需要另一个“第三个”foreach 来遍历 object 的属性。

Dim json = Await client.GetStringAsync(url)
Dim jss = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Object)(json) 

For Each property In jss("subject")
    Dim name1 = property.Name.ToString()
    Dim value1 = property.Value.ToString()

    ' ...
Next

For Each attribute In jss("attributes")
    For Each property In attribute
        Dim name1 = property.Name.ToString()
        Dim value1 = property.Value.ToString()

        ' ...
    Next
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM