使用不同的元素名称反序列化JSON

时间:2018-03-08 17:44:18

标签: vb.net json.net

我正在努力将json反序列化为带有newtonsoft的vb.net对象。问题是,以下数组的元素名称对于每个数组都是不同的:

{
    "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"]
        }
    }
]
}

如何将此特定json反序列化为vb.net对象?我正在为以下数组的不同名称而苦苦挣扎,比如“ABC”,“UZT”。

非常感谢你的帮助!

祝你好运 马丁

1 个答案:

答案 0 :(得分:0)

您几乎总能将数据映射到Dictionary(Of String, T),其中键/字符串将是属性名称,而T是用于保存数据的类。有一个简单的属性,可以是字符串,整数,日期等。这对于这类事情是理想的,但T将是一个类。课程:

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

如果您使用机器人制作这些(例如在Visual Studio中: 编辑 菜单,选择性粘贴将JSON粘贴为类 ),还有一些要做的清理工作。机器人不是非常聪明,所以他们制作Entity_Metadata1Entity_Metadata2等。由于化妆是相同的,你可以将其提炼为一个。

另一件事是数组。他们将创建:

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

但正确的语法应该是...As String()然后反序列化:

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

结果:

  

键:ABC,MD5:e24cb0e730269e419f036a10dd6c38d0,ndx end 3162
  键:UZT,MD5:dfed620a43ed7dcc2f0923337b9a75b0,ndx结束92
  key:NEQUZ,MD5:8b7bf4c2477ec72e0577aa5c968ffa1c,ndx end 3686
  密钥:NUTRF,MD5:a730b1bf89fd4da9986edeb931f3e507,ndx end 1133

实际上,由于ItemDataindex道具的数组,因此您可能需要或需要测试长度。