I have the following Json: http://pastebin.com/pd62g62w How can I deserialize "sensors"
and turn it into an array/list ? I'm using Json.NET for the deserialization.
DeviceModel deviceModel = new DeviceModel();
deviceModel = JsonConvert.DeserializeObject<DeviceModel>(json);
My current code:
using System;
using Newtonsoft.Json;
namespace Homecheck.Models {
public class DeviceModel {
public string error { get; set; }
public string errorType { get; set; }
[JsonProperty(PropertyName = "_id")]
public string id { get; set; }
[JsonProperty(PropertyName = "_user")]
public string user { get; set; }
[JsonProperty(PropertyName = "_serial")]
public string serial { get; set; }
}
}
Create a class for sensors objects
public class Sensor {
public bool Active { get; set; }
[JsonProperty(PropertyName = "_description")]
public bool Description { get; set; }
/* and so on */
}
And then add it to your model as IEnumerable<Sensor>
property:
public class DeviceModel {
/* existing properties omitted */
public IEnumerable<Sensor> Sensors { get; set; }
}
IEnumerable<Sensor>
gives you an interface to go through all elements. You can also use IList<Sensor>
if you want list semantics (add, remove, access by index), or use array Sensor[]
if you just want to have access by index.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.