Deserialize JSON with varying element names

老子叫甜甜 提交于 2019-12-18 09:14:50

问题


I'm struggling deserializing an json into an vb.net object with newtonsoft. The problem is, that the element name of the following array is different for each array:

{
    "ABC": [{
            "key": "123",
            "md5sum": "e24cb0e730269e419f036a10dd6c38d0",
            "entity_metadata": {
            "document_index_end": ["3162"],
            "document_index_start": ["3147"]
        }
    }, {
        "key": "456",
        "md5sum": "e24cb0e730269e419f036a10dd6c38d0",
        "entity_metadata": {
            "document_index_end": ["3162"],
            "document_index_start": ["3156"]
        }
    }
],
"UZT": [{
        "key": "074",
        "md5sum": "dfed620a43ed7dcc2f0923337b9a75b0",
        "entity_metadata": {
            "document_index_end": ["92"],
            "document_index_start": ["85"]
        }
    }
],
"NEQUZ": [{
        "key": "651",
        "md5sum": "8b7bf4c2477ec72e0577aa5c968ffa1c",
        "entity_metadata": {
            "document_index_end": ["3686"],
            "document_index_start": ["3663"]
        }
    }
],
"NUTRF": [{
        "key": "8422",
        "md5sum": "a730b1bf89fd4da9986edeb931f3e507",
        "entity_metadata": {
            "document_index_end": ["1133"],
            "document_index_start": ["1117"]
        }
    }, {
        "key": "5488",
        "md5sum": "a7aaff53e54d252ede34139e2f2404a1",
        "entity_metadata": {
            "document_index_end": ["1154"],
            "document_index_start": ["1151"]
        }
    }, {
        "key": "5522",
        "md5sum": "a7aaff53e54d252ede34139e2f2404a1",
        "entity_metadata": {
            "document_index_end": ["1163"],
            "document_index_start": ["1156"]
        }
    }
]
}

How can I deserialize this particular json to an vb.net object? I'm struggling about the different names of the following arrays, like "ABC", "UZT".

Thank you very much for your help!

Best regards Martin


回答1:


You can almost always map the data to a Dictionary(Of String, T) where the key/string will be the property name and the T is a class to hold the data. With a simple property, that could be string, int, date etc. It would be ideal for this sort of thing, but T would be a class. The classes:

Public Class ItemData
    Public Property key As String
    Public Property md5sum As String
    Public Property entity_metadata As EntityMetadata
End Class

Public Class EntityMetadata
    Public Property document_index_end As String()
    Public Property document_index_start As String()
End Class

If you use a robot to make these (such as in Visual Studio: Edit menu, Paste Special, Paste JSON as classes), there is a bit of cleanup to do. The robots are not terribly clever so they make Entity_Metadata1, Entity_Metadata2 etc. Since the make up is identical, you can distill it to just one.

Another thing is arrays. They will create:

Public Class Entity_Metadata###
    Public Property document_index_end() As String
    Public Property document_index_start() As String
End Class

But the correct syntax ought to be ...As String() Then deserialize:

Dim items = JsonConvert.DeserializeObject(Of Dictionary(Of String, ItemData()))(jstr)

' test/proof:
' the As... is important so that the Value gets cast correctly
For Each kvp As KeyValuePair(Of String, ItemData()) In items

    Console.WriteLine("key: {0}, MD5: {1}, ndx end {2}",
                      kvp.Key,
                      kvp.Value(0).md5sum,
                      kvp.Value(0).entity_metadata.document_index_end(0))
Next

Results:

key: ABC, MD5: e24cb0e730269e419f036a10dd6c38d0, ndx end 3162
key: UZT, MD5: dfed620a43ed7dcc2f0923337b9a75b0, ndx end 92
key: NEQUZ, MD5: 8b7bf4c2477ec72e0577aa5c968ffa1c, ndx end 3686
key: NUTRF, MD5: a730b1bf89fd4da9986edeb931f3e507, ndx end 1133

In practice, since ItemData is an array as are the index props, you may want or need to test the length.



来源:https://stackoverflow.com/questions/49179331/deserialize-json-with-varying-element-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!