0

无法为从服务接收的 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 进行反序列化

4

1 回答 1

0

试试这个:

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; }
}
于 2019-07-11T12:26:32.687 回答