在JSON.NET中解析JSON数组 [英] Parse JSON array in JSON.NET

查看:289
本文介绍了在JSON.NET中解析JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在REST API响应中有JSON对象:

I have JSON object in REST API response:

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

如果出现错误,他们将得到:

And if Error, they will get:

{
    Result: null,
    StatusCode: 404
}

我正在使用JSON.NET,并且已将类 DeviceInfo.cs

I'm using JSON.NET and I've class 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对象中的Result数组并将其存储在DeviceInfo类中?

And my question is how to parse Result array in JSON object and stored it in DeviceInfo class?

推荐答案

您需要属性帮助Newtonsoft.Json将源映射到您的类.

You need attributes help Newtonsoft.Json mapping the source to your class.

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的外部类.

And an outer class which your json was wrapped.

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

最后,您可以使用JsonConvert对您的json进行反序列化.

Finally, you can use JsonConvert to Deserialize your json.

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

这篇关于在JSON.NET中解析JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆