排列深度嵌套Json阵列窗口phone c#

本文关键字:窗口 phone 阵列 Json 深度 嵌套 排列 | 更新日期: 2023-09-27 18:28:26

我有以下JSON字符串:

{
    "response": {
        "hash": "5e042385c8e1d674056e53483828759e",
        "resp_code": "USER_SESSION_RETRIEVED",
        "code": 200
    },
    "structure": {
        "company": {
            "company_id": 3,
            "company_name": "The Ministry of Vaccines",
            "alarm_cnt": 4,
            "regions": [
                {
                    "sites": [
                        {
                            "assets": [
                                {
                                    "asset_id": 71,
                                    "asset_name": "Unassigned",
                                    "alarm_cnt": 0,
                                    "active_component_cnt": 1,
                                    "components": [
                                        {
                                            "latest_reading": "21.6°C",
                                            "component_id": 503,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Ambient",
                                            "latest_reading_tstamp": "2014-09-13 04:00:00"
                                        }
                                    ]
                                },
                                {
                                    "asset_id": 72,
                                    "asset_name": "Pharmacy fridge",
                                    "alarm_cnt": 0,
                                    "active_component_cnt": 2,
                                    "components": [
                                        {
                                            "latest_reading": "6.7°C",
                                            "component_id": 499,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Top",
                                            "latest_reading_tstamp": "2014-09-13 04:00:00"
                                        },
                                        {
                                            "latest_reading": "11.5°C",
                                            "component_id": 500,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Bottom",
                                            "latest_reading_tstamp": "2014-09-13 04:00:00"
                                        }
                                    ]
                                }
                            ],
                            "site_id": 35,
                            "site_name": "Acadia Pharmacy"
                        }
                    ],
                    "region_name": "Acadia Pharmacy",
                    "region_id": 25
                },
                {
                    "sites": [
                        {
                            "assets": [
                                {
                                    "asset_id": 73,
                                    "asset_name": "Cold room A",
                                    "alarm_cnt": 0,
                                    "active_component_cnt": 6,
                                    "components": [
                                        {
                                            "latest_reading": "-",
                                            "component_id": 339,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Probe 1",
                                            "latest_reading_tstamp": "-"
                                        },
                                        {
                                            "latest_reading": "-",
                                            "component_id": 333,
                                            "alarm_cnt": 0,
                                            "component_type": "Digital - Input",
                                            "component_name": "Door",
                                            "latest_reading_tstamp": "-"
                                        },
                                        {
                                            "latest_reading": "-",
                                            "component_id": 334,
                                            "alarm_cnt": 0,
                                            "component_type": "Digital - Input",
                                            "component_name": "Mains Power",
                                            "latest_reading_tstamp": "-"
                                        }
                                    ]
                                },
                                {
                                    "asset_id": 27,
                                    "asset_name": "Offices",
                                    "alarm_cnt": 0,
                                    "active_component_cnt": 3,
                                    "components": [
                                        {
                                            "latest_reading": "25.4°C",
                                            "component_id": 178,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Boardroom Temperature",
                                            "latest_reading_tstamp": "2014-08-18 12:50:00"
                                        },
                                        {
                                            "latest_reading": "Probe Err. (OC)",
                                            "component_id": 179,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "ICE 3 A1 -  Temperature",
                                            "latest_reading_tstamp": "2014-10-22 14:01:00",
                                            "sensor_error": true
                                        },
                                        {
                                            "latest_reading": "25.7°C",
                                            "component_id": 182,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "ICE3 - Ian's Office",
                                            "latest_reading_tstamp": "2014-10-22 14:01:00"
                                        }
                                    ]
                                },
                                {
                                    "asset_id": 26,
                                    "asset_name": "Peter Lamonts Fridge",
                                    "alarm_cnt": 3,
                                    "active_component_cnt": 5,
                                    "components": [
                                        {
                                            "latest_reading": "25.4°C",
                                            "component_id": 175,
                                            "alarm_cnt": 1,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Top Probe Temperature",
                                            "latest_reading_tstamp": "2014-08-18 12:50:00"
                                        },
                                        {
                                            "latest_reading": "25.4°C",
                                            "component_id": 176,
                                            "alarm_cnt": 1,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Mid Probe Temperature",
                                            "latest_reading_tstamp": "2014-08-18 12:50:00"
                                        },
                                        {
                                            "latest_reading": "25.4°C",
                                            "component_id": 177,
                                            "alarm_cnt": 1,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Bottom Probe Temperature",
                                            "latest_reading_tstamp": "2014-08-18 12:50:00"
                                        },
                                        {
                                            "latest_reading": "On",
                                            "component_id": 183,
                                            "alarm_cnt": 0,
                                            "component_type": "Digital - Input",
                                            "component_name": "Mains Power Peter Lamonts Fridge",
                                            "latest_reading_tstamp": "2014-08-18 12:50:00"
                                        },
                                        {
                                            "latest_reading": "Open",
                                            "component_id": 184,
                                            "alarm_cnt": 0,
                                            "component_type": "Digital - Input",
                                            "component_name": "Door Peter Lamonts Fridge",
                                            "latest_reading_tstamp": "2014-08-18 12:50:00"
                                        }
                                    ]
                                },
                                {
                                    "asset_id": 28,
                                    "asset_name": "Zero Fridge  Freezer",
                                    "alarm_cnt": 0,
                                    "active_component_cnt": 3,
                                    "components": [
                                        {
                                            "latest_reading": "Probe Err. (OC)",
                                            "component_id": 95,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Zero Freezer",
                                            "latest_reading_tstamp": "2014-08-18 12:20:00",
                                            "sensor_error": true
                                        },
                                        {
                                            "latest_reading": "Probe Err. (OC)",
                                            "component_id": 96,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Zero Fridge",
                                            "latest_reading_tstamp": "2014-08-18 12:20:00",
                                            "sensor_error": true
                                        },
                                        {
                                            "latest_reading": "",
                                            "component_id": 103,
                                            "alarm_cnt": 0,
                                            "component_type": "Digital - Input",
                                            "component_name": "Mains Power",
                                            "latest_reading_tstamp": "2014-08-18 12:20:00"
                                        }
                                    ]
                                }
                            ],
                            "site_id": 14,
                            "site_name": "21 Cradock Avenue"
                        }
                    ],
                    "region_name": "Rosebank",
                    "region_id": 3
                },
                {
                    "sites": [
                        {
                            "assets": [
                                {
                                    "asset_id": 44,
                                    "asset_name": "Dions Kitchen",
                                    "alarm_cnt": 0,
                                    "active_component_cnt": 1,
                                    "components": [
                                        {
                                            "latest_reading": "19.1°C",
                                            "component_id": 291,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "Ambient (Ice3)",
                                            "latest_reading_tstamp": "2014-10-22 21:51:00"
                                        }
                                    ]
                                },
                                {
                                    "asset_id": 31,
                                    "asset_name": "Vaccine Fridge",
                                    "alarm_cnt": 0,
                                    "active_component_cnt": 6,
                                    "components": [
                                        {
                                            "latest_reading": "30.3°C",
                                            "component_id": 87,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "A1:(Fridge) Ambient Temperature (BC540)",
                                            "latest_reading_tstamp": "2014-10-30 14:20:00"
                                        },
                                        {
                                            "latest_reading": "28.9°C",
                                            "component_id": 88,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "A2:Fridge Temp (BC540)",
                                            "latest_reading_tstamp": "2014-10-30 14:20:00"
                                        },
                                        {
                                            "latest_reading": "29.7°C",
                                            "component_id": 89,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "A3:Fridge (BC540)",
                                            "latest_reading_tstamp": "2014-10-30 14:20:00"
                                        },
                                        {
                                            "latest_reading": "6.6°C",
                                            "component_id": 21,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "ICE3 - A1 Fridge section",
                                            "latest_reading_tstamp": "2014-10-22 21:51:00"
                                        },
                                        {
                                            "latest_reading": "-14.9°C",
                                            "component_id": 22,
                                            "alarm_cnt": 0,
                                            "component_type": "Analogue - Input",
                                            "component_name": "ICE3 - A2 Freezer section",
                                            "latest_reading_tstamp": "2014-10-22 21:51:00"
                                        },
                                        {
                                            "latest_reading": "",
                                            "component_id": 25,
                                            "alarm_cnt": 0,
                                            "component_type": "Digital - Input",
                                            "component_name": "Mains Power",
                                            "latest_reading_tstamp": "2014-10-30 14:20:00"
                                        }
                                    ]
                                }
                            ],
                            "site_id": 2,
                            "site_name": "37 Wandel Ave"
                        }
                    ],
                    "region_name": "Sandton",
                    "region_id": 2
                }
            ]
        }
    }
}

我使用json2csharp:创建了这些类

    public class Response
    {
        public string hash { get; set; }
        public string resp_code { get; set; }
        public int code { get; set; }
    }
    public class Component
    {
        public string latest_reading { get; set; }
        public int component_id { get; set; }
        public int alarm_cnt { get; set; }
        public string component_type { get; set; }
        public string component_name { get; set; }
        public string latest_reading_tstamp { get; set; }
        public bool? sensor_error { get; set; }
    }
    public class Asset
    {
        public int asset_id { get; set; }
        public string asset_name { get; set; }
        public int alarm_cnt { get; set; }
        public int active_component_cnt { get; set; }
        public List<Component> components { get; set; }
    }
    public class Site
    {
        public List<Asset> assets { get; set; }
        public int site_id { get; set; }
        public string site_name { get; set; }
    }
    public class Region
    {
        public List<Site> sites { get; set; }
        public string region_name { get; set; }
        public int region_id { get; set; }
    }
    public class Company
    {
        public int company_id { get; set; }
        public string company_name { get; set; }
        public int alarm_cnt { get; set; }
        public List<Region> regions { get; set; }
    }
    public class Structure
    {
        public Company company { get; set; }
    }
    public class RootObject
    {
        public Response response { get; set; }
        public Structure structure { get; set; }
    }

以下是我到目前为止所做的

var client = new RestClient() { BaseUrl = url };
var request = new RestRequest()
{ 
    Resource = "", 
    Method = Method.GET, 
    RequestFormat = DataFormat.Json 
};
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
client.ExecuteAsync(request, (response, handle) =>
{
    RestSharp.Deserializers.JsonDeserializer deserial = 
        new RestSharp.Deserializers.JsonDeserializer();
    var resp = deserial.Deserialize<List<RootObject>>(response);
    var JSONObj = deserial.Deserialize<Dictionary<string, string>>(response);
    string structure = JSONObj["structure"].ToString();
}

这给了我"结构"下的所有数据

我需要将RegionNames列在列表框中;我需要得到每个地区的网站列表;我还需要根据宝贵的选择字段,列出每个网站下的资产。

我现在的问题是使用数据。我使用RestSharp客户端。我必须相应地对数据进行分组:所有区域、站点、资产。站点必须按照其所属的区域进行分组;资产也必须按照其所属的站点进行分组。有人知道我该怎么做吗?如有任何帮助,我们将不胜感激。

排列深度嵌套Json阵列窗口phone c#

JSON表示JavaScript对象符号。

要反序列化,您应该创建一个C#对象,它看起来像那个JSON对象(相同的属性名,相同的数据类型)。

然后可以使用反序列化对象

new JavaScriptSerializer().Deserialize<MyJSONObjectType>(jsonstring);

例如,JSON字符串

{success:true,data:[{id:1,name:'Sam'},{id:2,name:'Jim'}]}

可以反序列化为对象

public class MyJSONObjectType {
    public bool success;
    public List<MySubObj> data;
}
public class MySubObj {
    public int id;
    public string name;
}