Deserialize List JSON C #

0

In my program I make 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 these data interest me, I want to make a list that contains the ID's of the filtered tickets. That is, inside 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 fields "2" that contain the ID's:

"2":8283 e "2":8282

I want to create a list with those two Id's, but I do not know how to filter the data to just get the Id's in a list, someone can help me:

    
asked by anonymous 02.04.2018 / 12:34

1 answer

2

One of the ways it can be done is as follows.

Create a class that will serve as a template for your json . This template is based on the json you get in 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; }
}

Notice that I define an Attribute named PropertyName to indicate which "field" of json will come the information: totalcount , count , sort . In your case, what you want is the data property, but I put all of them in order of organization.

Once you have done this, create the class Data which will store the information you want from the json field and have the% / p>

using Newtonsoft.Json;

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

Notice that I am also using the Attribute Data to indicate the name of the field that has ID , in this case PropertyName .

When the structure is mounted, you can read your json as follows, where ID is the result of your GET:

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

In 2 you will have a list of json objects, where each will have the result you requested.

Other fields

If you want to search data from other fields of your Data , such as from the ID field, simply modify the json class 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; }
}

Just be aware of typing the fields, if they are teste , Data , etc.

    
02.04.2018 / 13:11