2
{
"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

Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
Chazman88
  • 23
  • 3
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Matthias Burger Aug 15 '17 at 10:09
  • 1
    in your json, the property `list` must be named `device` or rename the property `device` using `[JsonProperty("list")]` - and your json is invalid. you're missing a `}` at the end – Matthias Burger Aug 15 '17 at 10:12

2 Answers2

4

in your json, the property list must be named device or rename the property device using [JsonProperty("list")]:

public class DeviceStatus
{
    public int totalDevices { get; set; }
    public int startIndex { get; set; }
    public int utcTimestamp { get; set; }

    [JsonProperty("list")]
    public List<Device> device { get; set; }
}

otherwise Json.Net doesn't know which property list belongs to and the value is null - here you get the NullReferenceException you mentioned

Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
0

As Matthias Burger said, the name of your "list" doesn't match, hence your device member will be empty (or even null, in this case, I think).

Erik Bongers
  • 416
  • 2
  • 10