2017-08-15 209 views
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; } 
} 

我試圖反序列化從一個以太網設備接收的JSON字符串。我收到的JSON字符串在上面。C#JSON反序列化對象內的對象列表

我已經制定了如何反序列化才能到DeviceStatus.totalDevices,.startIndex,.utcTimestamp等

但是當我做了Console.WriteLine(string.Format("Device 1 ID = {0}",jsonRootObj.deviceStatus.device[0].identifier)); 我得到一個異常

未將對象引用設置爲一個對象的實例

我確定我缺少一些非常簡單的東西,但這是我第一次使用C#項目,所以無法解決這個問題。

我已經做了很多搜索,在這裏讓我到這一點,但不能進一步。

感謝

+1

可能重複[什麼是NullReferenceException,以及如何解決它?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix -it) –

+1

在你的json中,屬性'list'必須被命名爲'device'或者使用'[JsonProperty(「list」)]來重命名'device'屬性,並且你的json是無效的。你最後錯過了一個'}' –

回答

4

在你的JSON,使用[JsonProperty("list")]財產list必須命名爲device或重命名屬性device

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; } 
} 

否則Json.Net不知道哪個屬性list屬於和值爲空 - 在這裏你得到你提到的NullReferenceException

0

正如Matthias Burger所說,你的名字「list 「不匹配,因此您的device成員將是空的(或者甚至爲空,在這種情況下,我認爲)。