2014-10-29 43 views
-2

我有以下JSON字符串:安排深層嵌套的JSON數組的Windows Phone C#

{ 
    "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客戶端。 我必須對數據進行相應分組:所有地區,網站,資產。該網站必須歸入其所屬的地區;資產也必須歸入它們所屬的網站。有誰知道我該怎麼做?任何幫助,將不勝感激。

+0

喜請點擊此鏈接HTTP反序列化對象: //197.96.22.218:8080/Phase3JEE-war/v1.0/614b0c4d3f78f1e0c8cb8661aa657a05/user_session/?sections=app-basic&company_id=3 – 2014-10-30 06:08:31

+0

我已經編輯了您的問題,以便從您的鏈接中包含重新格式化的JSON。我將其中的一部分剪掉以使其符合字符限制。其餘的JSON與jsonlint.com一起驗證,我認爲仍然會傳達您正在使用的結構。 – 2014-10-30 14:44:34

+0

thanx布萊恩..現在你有什麼想法我可以如何完成我需要用這個來完成? – 2014-11-03 15:06:13

回答

2

JSON表示JavaScript 對象表示法。

要反序列化,您應該創建一個看起來像JSON對象(相同的屬性名稱,理智的數據類型)的C#對象

然後你就可以用

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; 
} 
+0

thanx ..但ive反序列化的JSON響應..我的問題是安排數據和摸索它在相關的列表框..我使用RestSharp.Deserializers.JsonDeserializer – 2014-10-29 12:15:14

+0

所以,實際上,你的問題是絕對不清楚,如果沒有誤導。請以您能夠理解您的問題的方式發佈您的問題。尤其是,發佈你的代碼。 – Alexander 2014-10-29 12:46:26

+0

我編輯了我的問題,幷包含了我的代碼,希望我現在可以立即使用 – 2014-10-29 18:28:28