Deserialize List JSON C#

Asked

Viewed 1,841 times

0

In my program I do a GET and get the following:

 {"totalcount":2,"count":2,"sort":1,"order":"ASC","data":[{"2":8283,"1":"Teste","12":6,"15":"2018-03-29 11:30:04","19":"2018-03-29 15:13:18","10":5,"4":981,"5":981,"8":null,"159":0,"7":"Others > Management","18":null,"82":0},{"2":8282,"1":"TESTE AFD","12":5,"15":"2018-03-29 11:14:24","19":"2018-04-02 08:58:50","10":3,"4":981,"5":981,"8":null,"159":0,"7":"Services > I need to bill a new client without a software license.","18":null,"82":0}],"content-range":"0-1/2"}

But not all of this data interests me, I want to make a list that contains the ID’s of the filtered tickets. That is, within this json there is a field called date, which contains the following:

"data":[{"2":8283,"1":"Teste","12":6,"15":"2018-03-29 11:30:04","19":"2018-03-29 15:13:18","10":5,"4":981,"5":981,"8":null,"159":0,"7":"Others > Management","18":null,"82":0},{"2":8282,"1":"TESTE AFD","12":5,"15":"2018-03-29 11:14:24","19":"2018-04-02 08:58:50","10":3,"4":981,"5":981,"8":null,"159":0,"7":"Services > I need to bill a new client without a software license.","18":null,"82":0}]

And within the date there are the "2" fields that contain the ID’s:

"2":8283 e "2":8282

I want to create a list of those two Id’s, but I don’t know how to filter the data to get only the Id’s on a list, someone can help me:

1 answer

2


One of the ways that can be done is this.

Create a class that will serve as your model json. This model is based on json that you get on the GET you mentioned.

using Newtonsoft.Json;

public class JsonModel
{
    [JsonProperty(PropertyName = "totalcount")]
    public int TotalCount { get; set; }

    [JsonProperty(PropertyName = "count")]
    public int Sort { get; set; }

    [JsonProperty(PropertyName = "sort")]
    public string Order { get; set; }

    [JsonProperty(PropertyName = "data")]
    public List<Data> Data { get; set; }
}

Realize that I define a Attribute called PropertyName to indicate which "field" of the json will come the information: totalcount, count, sort and data. In your case, what you want is the property Data, but I put all for organization.

Done this, create the class Data which will store the information you want, coming from the "date" field of your json and who will own the ID desiring:

using Newtonsoft.Json;

public class Data
{
    [JsonProperty(PropertyName = "2")]
    public int ID { get; set; }
}

Note that I am also using the Attribute PropertyName to specify the name of the field that has the ID, in your case 2.

Mounted the structure, you can read your json as follows, where json is the result obtained on your GET:

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

In result you will have a list of objects Data, where each in turn will have the ID who requested.

Other fields

If you want to search the data from other fields of your json, such as the field teste, just modify the class Data to the following:

using Newtonsoft.Json;

public class Data
{
    [JsonProperty(PropertyName = "2")]
    public int ID { get; set; }

    [JsonProperty(PropertyName = "teste")]
    public string Test { get; set; }
}

Stay tuned only for the typing of the fields, if they are int, string, etc..

  • 1

    Perfect, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.