Json 反序列化 key/value 字典
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
看来反序列化字典的方式不对。
错误是
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'Document' because the type requires a JSON object (e.g.
{"name":"value"}) to deserialize correctly. To fix this error either
change the JSON to a JSON object (e.g. {"name":"value"}) or change the
deserialized type to an array or a type that implements a collection
interface (e.g. ICollection, IList) like List that can be
deserialized from a JSON array. JsonArrayAttribute can also be added
to the type to force it to deserialize from a JSON array.
到目前为止我尝试过的:
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
当我试图弄明白却没有答案时,这让我发疯了。对我有用的是我从 class 中的数组和列表中删除了“属性”,只留下了 Public 关键字。
我看到了“类似问题”,但尝试了所有问题都没有解决我的问题。 我有一个这样的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
看来反序列化字典的方式不对。 错误是
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Document' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
到目前为止我尝试过的:
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
当我试图弄明白却没有答案时,这让我发疯了。对我有用的是我从 class 中的数组和列表中删除了“属性”,只留下了 Public 关键字。