2016-12-26 114 views
1

我在REST API響應JSON對象:解析JSON數組中JSON.NET

{ 
    Result: 
    [ 
     { 
      "id": 1, 
      "id_endpoint": 1, 
      "name": "Endpoint 1", 
      "description": "Endpoint 1", 
      "unit": "mmol", 
      "minthreshold": 30, 
      "maxthreshold": -15, 
      "id_device": 4, 
      "value": 7, 
      "time": "2016-12-24T21:20:19.000Z", 
      "address": "Endpoint 1", 
      "id_user": 1 
     }, { 
      "id": 2, 
      "id_endpoint": 1, 
      "name": "Endpoint 1", 
      "description": "Endpoint 1", 
      "unit": "mmol", 
      "minthreshold": 30, 
      "maxthreshold": -15, 
      "id_device": 4, 
      "value": 6, 
      "time": "2016-12-24T21:20:16.000Z", 
      "address": "Endpoint 1", 
      "id_user": 1 
     }, { 
      "id": 3, 
      "id_endpoint": 1, 
      "name": "Endpoint 1", 
      "description": "Endpoint 1", 
      "unit": "mmol", 
      "minthreshold": 30, 
      "maxthreshold": -15, 
      "id_device": 4, 
      "value": 8, 
      "time": "2016-12-24T21:18:38.000Z", 
      "address": "Endpoint 1", 
      "id_user": 1 
     } 
    ], 
    StatusCode: 200 
} 

如果錯誤,他們將獲得:

{ 
    Result: null, 
    StatusCode: 404 
} 

我使用JSON.NET和我「五個一流DeviceInfo.cs

public class DeviceInfo 
{ 
    public int DeviceID {get;set;} 
    public int EndpointID {get;set;} 
    public string DeviceName {get;set;} 
    public double MinThreshold {get;set;} 
    public double MaxThreshold {get;set;} 
    public double CurrentValue {get;set;} 
    public DateTime ValueTime {get;set;} 
    public string EndpointAddress {get;set;} 
    public int IDUser {get;set;} 
} 

我的問題是如何解析結果JSON objec陣列t並將其存儲在DeviceInfo類中?

回答

5

您需要屬性幫助Newtonsoft.Json將源映射到您的類。

public class DeviceInfo 
{ 
    [JsonProperty("id")] 
    public int DeviceID { get; set; } 
    [JsonProperty("id_endpoint")] 
    public int EndpointID { get; set; } 
    [JsonProperty("name")] 
    public string DeviceName { get; set; } 
    [JsonProperty("minthreshold")] 
    public double MinThreshold { get; set; } 
    [JsonProperty("maxthreshold")] 
    public double MaxThreshold { get; set; } 
    [JsonProperty("value")] 
    public double CurrentValue { get; set; } 
    [JsonProperty("time")] 
    public DateTime ValueTime { get; set; } 
    [JsonProperty("address")] 
    public string EndpointAddress { get; set; } 
    [JsonProperty("id_user")] 
    public int IDUser { get; set; } 
} 

和你的JSON包裹外部類。

public class RootObject 
{ 
    public List<DeviceInfo> Result { get; set; } 
    public int StatusCode { get; set; } 
} 

最後,你可以使用JsonConvert來反序列化你的json。

var result = JsonConvert.DeserializeObject<RootObject>(json); 
+0

thanks!是工作! –

0

您可以使用JsonDeserialize解析JSON

var array = JsonConvert.DeserializeObject<List<DeviceInfo>>(str); 
0

請參考下面的代碼:

var response = JsonConvert.DeserializeObject<Dictionary<string,object>>(JSONstring); 

if(response != null && response["StatusCode"] == "200") 
{ 
    List<DeviceInfo> lstResult = JsonConvert.DeserializeObject<List<DeviceInfo>(response["Result"]); 
} 

我希望這會有所幫助。