Windows Phone 8 Silverlight JSON取消密封



我想在我的Windows Phone 8.1 Silverlight项目中解析一些json数据。我创建了类并反序列化对象。每次返回空值。我哪里做错了?提前感谢。。。

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>1t1,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;

看起来您将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"]

最新更新