使用不同的元素名称反序列化JSON [英] Deserialize JSON with varying element names

查看:113
本文介绍了使用不同的元素名称反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如何将这个特定的json反序列化为vb.net对象?我正在努力应对以下数组的不同名称,例如"ABC","UZT".

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".

非常感谢您的帮助!

最诚挚的问候 马丁

推荐答案

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

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

如果您使用机器人制作这些文件(例如在Visual Studio中: 编辑菜单,粘贴特殊将JSON粘贴为类 ),需要做一些清理工作.机器人不是很聪明,所以他们制作Entity_Metadata1Entity_Metadata2等.由于组成相同,因此您可以将其精炼成一个.

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

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

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

结果:

密钥:ABC,MD5:e24cb0e730269e419f036a10dd6c38d0,ndx末端3162
键:UZT,MD5:dfed620a43ed7dcc2f0923337b9a75b0,ndx端92
密钥:NEQUZ,MD5:8b7bf4c2477ec72e0577aa5c968ffa1c,ndx端3686
密钥:NUTRF,MD5:a730b1bf89fd4da9986edeb931f3e507,ndx结尾1133

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

实际上,由于ItemData是一个数组,与index道具一样,因此您可能需要或需要测试其长度.

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

这篇关于使用不同的元素名称反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆