c# - 在 JSON.NET 中解析 JSON 数组

标签 c# json json.net

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

{
    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 对象中的结果数组并将其存储在 DeviceInfo 类中?

最佳答案

您需要属性帮助 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);

关于c# - 在 JSON.NET 中解析 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41327146/

相关文章:

c# - 引用类型的 ref 修饰符

c# - 即使属性存在,编译时也会出现 CS1061 错误

java - GSON 只解析对象名称

jquery - 返回需要执行的javascript,是否使用json?

java - 当JSON包含Json.Net C#生成的$ref和$id时,如何在Java中反序列化

c# - XmlDocument读取XML文档注释问题

c# - NHibernate 的独立 Hello World 类型示例

c# - 不支持的媒体类型 ASP.NET Core Web API

c# - Newtonsoft.Json DeserializeObject 为 guid 00000000-0000-0000-0000-000000000000 传递 null

c# - 子属性的 JSON .NET 自定义名称解析器