繁体   English   中英

Json 反序列化键/值字典

[英]Json deserialize key/value dictionary

我看到了“类似的问题”,但尝试了所有这些并没有解决我的问题。 我有一个像这样的 Json

{
    "externalId": "pobj124",
    "dossierNumber": "Test 20210216",
    "issuedDate": "2021-01-28T23:00:00+00:00",
    "issuedPlace": "New-York",
    "properties": {
        "documentTypeId": 7,
        "countryLayout": "US"
    }
}

属性是未知数量的“名称”:“值”。 不知道名称和价值。 我的 class 定义为

<Serializable>
Public Class Document
    Public Property externalId As String
    Public Property dossierNumber As String
    Public Property issuedDate As Date
    Public Property issuedPlace As String
    Public Property properties As IDictionary(Of String, String)
End Class

调用代码是

Private Function DeserializeJson(json As String) As Document
    Return JsonConvert.DeserializeObject(Of Document)(json)
End Function

似乎这不是反序列化字典的正确方法。 错误是

无法将当前的 JSON 数组(例如 [1,2,3])反序列化为类型“文档”,因为该类型需要 JSON object (例如正确地反序列化“名称”“值}” 要修复此错误,请将 JSON 更改为 JSON object (例如 {"name":"value"})或将实现的反序列化类型接口更改为数组或集合从 JSON 阵列反序列化。 JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化。

到目前为止我已经尝试过:

Public Property properties As IDictionary(Of String, String)
Public Property properties As List(Of KeyValuePair(Of String, String))
Public Property properties As Dictionary(Of String, String)
Public Property properties() As KeyValuePair(Of String, String)

在 vb.net 上尝试过类似下面的东西。 使用 BsonTypeMapper.MapToDotNetValue。

JsonConvert.DeserializeObject(BsonTypeMapper.MapToDotNetValue( a(i) .ToString().Replace("\n", "||")))

错误出现在底层类的定义中。 一个被定义为

Public Class PartialClass
    Public Property goods() As Product
End Class

Public Class Product
    Public Property caracteristics As IDictionnary(Of String, String)
End Class

什么时候应该

Public Class PartialClass
    Public Property goods As List(Of Product)
End Class

当我试图在没有答案的情况下解决这个问题时,这让我发疯了。 对我有用的是我从 Arrays 和 class 中的列表中删除了“属性”,只留下了公共关键字。

暂无
暂无

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

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