简体   繁体   English

Json 反序列化键/值字典

[英]Json deserialize key/value dictionary

I saw the "Similar question" but trying all of them did not solve my problem.我看到了“类似的问题”,但尝试了所有这些并没有解决我的问题。 I have a Json like this我有一个像这样的 Json

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

The properties is an unknown number of "name": "value".属性是未知数量的“名称”:“值”。 Not knowing neither the name nor the value.不知道名称和价值。 My class is defined as我的 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

The calling code is调用代码是

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

It seems that it is not the right way to deserialize a dictionary.似乎这不是反序列化字典的正确方法。 Error is错误是

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'Document' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.无法将当前的 JSON 数组(例如 [1,2,3])反序列化为类型“文档”,因为该类型需要 JSON object (例如正确地反序列化“名称”“值}” To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array.要修复此错误,请将 JSON 更改为 JSON object (例如 {"name":"value"})或将实现的反序列化类型接口更改为数组或集合从 JSON 阵列反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化。

What I've tried so far:到目前为止我已经尝试过:

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)

have tried Something similar like below on vb.net.在 vb.net 上尝试过类似下面的东西。 Use BsonTypeMapper.MapToDotNetValue.使用 BsonTypeMapper.MapToDotNetValue。

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

The error was in the definition of the underlying classes.错误出现在底层类的定义中。 One was defined as一个被定义为

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

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

when it should be什么时候应该

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

This drove me to insanity when I was trying to figure it out with no answers.当我试图在没有答案的情况下解决这个问题时,这让我发疯了。 What worked for me is I removed “Property” from Arrays and Lists within the class and left the Public Keyword only.对我有用的是我从 Arrays 和 class 中的列表中删除了“属性”,只留下了公共关键字。

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

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