簡體   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