简体   繁体   English

将JSON从多个对象反序列化为对象列表

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

Unable to create mapping object / response model for the received JSON response from the service. 无法为从服务接收到的JSON响应创建映射对象/响应模型。

Tried of creating custom JsonConverter but could not find any appropriate method which can help to resolve the Deserializing issue 尝试创建自定义JsonConverter,但找不到任何合适的方法来帮助解决反序列化问题

The following is the JSON response which i am getting from the Service. 以下是我从服务获得的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"
    }
}

Example 1:- Same service can return below JSON format 示例1:-相同的服务可以返回以下JSON格式

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

Create c# root object class so that it can easily used to Deserializing using JsonConvert 创建c#根对象类,以便可以使用JsonConvert轻松地对它进行反序列化

Try this one: 试试这个:

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