2015-05-12 57 views
0

我想解析Windows Phone 8.1 Silverlight項目中的一些json數據。我創建了類並反序列化了對象。每次它返回空值。我在哪裏做錯了?在此先感謝...Windows Phone 8 Silverlight JSON Deserealize

JSON

{ "full": 1, "version": "*1.7.569*", "model": "MiCasaVerde VeraLite",    "zwave_heal": 1, "temperature": "C", "skin": "mios", "serial_number":  "35101436", "fwd1": "vera-us-oem-relay31.mios.com", "fwd2": "vera-us-oem- relay12.mios.com", "mode": 2, "sections": [ { "name": "My Home", "id": 1 } ],  "rooms": [ ], "scenes": [ ], "devices": [ { "name": "Duman Sensoru", "altid":  "11", "id": 24, "category": 4, "subcategory": 4, "room": 0, "parent": 1, "armed": "1", "tripped": "0", "armedtripped": "0", "lasttrip": "1430755895", "batterylevel": "100", "temperature": "26" }, { "name": "Hareket Sensoru", "altid": "9", "id": 19, "category": 4, "subcategory": 3, "room": 0, "parent": 1, "armed": "1", "tripped": "0", "lasttrip": "1430779873", "armedtripped": "0", "batterylevel": "94", "temperature": "24", "light": "0", "humidity": "51" }, { "name": "Isik Sensoru", "altid": "m3", "id": 21, "category": 18, "subcategory": 0, "room": 0, "parent": 19, "light": "0" }, { "name": "Kapı Kilidi", "altid": "10", "id": 23, "category": 7, "subcategory": 0, "room": 0, "parent": 1, "pincodes": "<VERSION=3>1\t1,0;\t2,0;\t3,0;\t4,0;\t5,0;\t6,0;\t7,0;\t8,0;\t9,0;\t10,0;\t11,0;\t12,0;\t13,0;\t14,0;\t15,0;\t16,0;\t17,0;\t18,0;\t19,0;\t20,0;\t", "locked": "1", "batterylevel": "96", "state": -1, "comment": "" }, { "name": "Nem Sensoru", "altid": "m5", "id": 22, "category": 16, "subcategory": 0, "room": 0, "parent": 19, "humidity": "51" }, { "name": "On\/Off Switch", "altid": "8", "id": 18, "category": 3, "subcategory": 0, "room": 0, "parent": 1, "status": "1", "state": -1, "comment": "" }, { "name": "Pencere&Kapi Sensoru", "altid": "7", "id": 10, "category": 4, "subcategory": 1, "room": 0, "parent": 1, "armed": "1", "tripped": "0", "lasttrip": "1419667909", "armedtripped": "0", "batterylevel": "88" }, { "name": "Priz", "altid": "2", "id": 3, "category": 3, "subcategory": 0, "room": 0, "parent": 1, "status": "0", "watts": "0", "kwh": "0.0000", "state": -1, "comment": "" }, { "name": "Temperature Sensor", "altid": "m1", "id": 20, "category": 17, "subcategory": 0, "room": 0, "parent": 19, "temperature": "24" }, { "name": "Termometre", "altid": "m1", "id": 25, "category": 17, "subcategory": 0, "room": 0, "parent": 24, "temperature": "26" } ], "categories": [ { "name": "On\/Off Switch", "id": 3 }, { "name": "Sensor", "id": 4 }, { "name": "Doorlock", "id": 7 }, { "name": "Humidity Sensor", "id": 16 }, { "name": "Temperature Sensor", "id": 17 }, { "name": "Light Sensor", "id": 18 } ], "ir": 0, "irtx": "", "loadtime": 1431434160, "dataversion":  434160216, "state": -1, "comment": "" } 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace deneme 
{ 
    class Zwave 
    { 
     public Section section { get; set; } 
     public Device device { get; set; } 
     public Category category { get; set; } 
     public RootObject rootobject { get; set; } 

     public class Section 
     { 
      public string name { get; set; } 
      public int id { get; set; } 
     } 

     public class Device 
     { 
      public string name { get; set; } 
      public string altid { get; set; } 
      public int id { get; set; } 
      public int category { get; set; } 
      public int subcategory { get; set; } 
      public int room { get; set; } 
      public int parent { get; set; } 
      public string armed { get; set; } 
      public string tripped { get; set; } 
      public string armedtripped { get; set; } 
      public string lasttrip { get; set; } 
      public string batterylevel { get; set; } 
      public string temperature { get; set; } 
      public string light { get; set; } 
      public string humidity { get; set; } 
      public string pincodes { get; set; } 
      public string locked { get; set; } 
      public int? state { get; set; } 
      public string comment { get; set; } 
      public string status { get; set; } 
      public string watts { get; set; } 
      public string kwh { get; set; } 
     } 

     public class Category 
     { 
      public string name { get; set; } 
      public int id { get; set; } 
     } 

     public class RootObject 
     { 
      public int full { get; set; } 
      public string version { get; set; } 
      public string model { get; set; } 
      public int zwave_heal { get; set; } 
      public string temperature { get; set; } 
      public string skin { get; set; } 
      public string serial_number { get; set; } 
      public string fwd1 { get; set; } 
      public string fwd2 { get; set; } 
      public int mode { get; set; } 
      public List<Section> sections { get; set; } 
      public List<object> rooms { get; set; } 
      public List<object> scenes { get; set; } 
      public List<Device> devices { get; set; } 
      public List<Category> categories { get; set; } 
      public int ir { get; set; } 
      public string irtx { get; set; } 
      public int loadtime { get; set; } 
      public int dataversion { get; set; } 
      public int state { get; set; } 
      public string comment { get; set; } 
     } 
    } 
} 

功能

HttpClient httpClient = new HttpClient(); 
       var json = await httpClient.GetStringAsync(new Uri("http://xx.xxx.xxx.xxx:xxxx/data_request?id=lu_sdata")); 
       Zwave returned=JsonConvert.DeserializeObject<Zwave>(json); 
       var listed = returned.device.name; 

回答

0

看起來你發佈你的JSON到http://json2csharp.com/和提供使用的類 - - 目前很好。但是,然後你將它們嵌套在一些外部類Zwave中,並試圖反序列化,這是不正確的。你需要做的:

var returned = JsonConvert.DeserializeObject<Zwave.RootObject>(json); 

或者更好的是,從Zwave容器類中刪除,並做

var returned = JsonConvert.DeserializeObject<RootObject>(json); 

接下來,你的JSON包含"device"對象的數組 - 10 - 事實上這得到反序列化爲List<Device>。所以,如果你想他們的名字,你需要做的:

var listed = returned.devices.Select(d => d.name).ToList(); 

這給:

["Duman Sensoru","Hareket Sensoru","Isik Sensoru","Kapı Kilidi","Nem Sensoru","On/Off Switch","Pencere&Kapi Sensoru","Priz","Temperature Sensor","Termometre"]