Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

c# - Parse JSON array in JSON.NET

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
}

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

And an outer class which your json was wrapped.

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

Finally, you can use JsonConvert to Deserialize your json.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...