簡體   English   中英

將JSON從多個對象反序列化為對象列表

[英]Deserialize JSON from multiple objects to list of objects

無法為從服務接收到的JSON響應創建映射對象/響應模型。

嘗試創建自定義JsonConverter,但找不到任何合適的方法來幫助解決反序列化問題

以下是我從服務獲得的JSON響應。

{
    "$base": "Collection",
    "nodeType": "PROTOCOL",
    "Smart Building 0": {
        "$base": "Collection",
        "nodeType": "NETWORK",
        "truncated": "true"
    },
    "Smart Building 1": {
        "$base": "Collection",
        "nodeType": "NETWORK",
        "truncated": "true"
    },
    "Smart Building 2": {
        "$base": "Collection",
        "nodeType": "NETWORK",
        "truncated": "true"
    }
}

示例1:-相同的服務可以返回以下JSON格式

{
    "$base": "Collection",
    "nodeType": "PROTOCOL",
    "Smart Building 0": {
        "$base": "Collection",
        "nodeType": "NETWORK",
        "truncated": "true"
    },
    "Smart Building 1": {
        "$base": "Collection",
        "nodeType": "NETWORK",
        "truncated": "true"
    }
}

創建c#根對象類,以便可以使用JsonConvert輕松地對它進行反序列化

試試這個:

public class Root : Dictionary<string, Building>
{   
    [JsonProperty("$base")]
    public string Base { get; set; }

    [JsonProperty("nodeType")]
    public string NodeType { get; set; }
}

public class Building
{
    [JsonProperty("$base")]
    public string Base { get; set; }

    [JsonProperty("nodeType")]
    public string NodeType { get; set; }

    [JsonProperty("truncated")]
    public string Truncated { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM