{
"deviceStatus": {
"totalDevices": 3,
"startIndex": 0,
"utcTimestamp": 1502782784,
"list": [
{
"identifier": "000D6F000A9E6E3D:0",
"rxTime": 0
},
{
"identifier": "000D6F000BE977F0:0",
"rxTime": 1502782323,
"lowBattery": "false",
"level": "0",
"batteryLevel": "84"
},
{
"identifier": "000D6F000BE95E24:0",
"rxTime": 1502782754,
"lowBattery": "false",
"level": "0",
"batteryLevel": "86"
}
]
}
}
public class Qube
{
private const string _JSON = "{\"deviceStatus\":{\"totalDevices\":3,\"startIndex\":0,\"utcTimestamp\":1502782784,\"list\":[{\"identifier\":\"000D6F000A9E6E3D:0\",\"rxTime\":0},{\"identifier\":\"000D6F000BE977F0:0\",\"rxTime\":1502782323,\"lowBattery\":\"false\",\"level\":\"0\",\"batteryLevel\":\"84\"},{\"identifier\":\"000D6F000BE95E24:0\",\"rxTime\":1502782754,\"lowBattery\":\"false\",\"level\":\"0\",\"batteryLevel\":\"86\"}]}}";
public void GetStatus()
{
var jsonRootObj = JsonConvert.DeserializeObject<RootObject>(_JSON);
Console.WriteLine(string.Format("Total Devices = {0}, Start Index = {1}, Timestamp = {2}",jsonRootObj.deviceStatus.totalDevices,jsonRootObj.deviceStatus.startIndex,jsonRootObj.deviceStatus.utcTimestamp));
Console.WriteLine(string.Format("Device 1 ID = {0}",jsonRootObj.deviceStatus.device[0].identifier));
Console.ReadLine();
}
}
public class Device
{
public string identifier { get; set; }
public int rxTime { get; set; }
public string lowBattery { get; set; }
public string level { get; set; }
public string batteryLevel { get; set; }
}
public class DeviceStatus
{
public int totalDevices { get; set; }
public int startIndex { get; set; }
public int utcTimestamp { get; set; }
public List<Device> device { get; set; }
}
public class RootObject
{
public DeviceStatus deviceStatus { get; set; }
}
Im trying to Deserialize the JSON string that I receive from a ethernet device. The JSON string that I receive is above.
I have worked out how to deserialize to get to the DeviceStatus.totalDevices, .startIndex, .utcTimestamp etc.
But when i do the Console.WriteLine(string.Format("Device 1 ID = {0}",jsonRootObj.deviceStatus.device[0].identifier));
I get an exception
Object reference not set to an instance of an object
Im sure that im missing something very simple but this is my first ever C# project so cant work it out.
I've done quite alot of searching on here to get me to this point but just cant get any further.
Thanks