How to get items from a json in C #

-1

After almost 3 days trying to implement I was not successful. I have the following json:

{ "code": 200,
 "data": [ 
 { "id": 1, "type": "charge", "custom_id": "1208", "status": { "current": "new", "previous": null }, "identifiers": { "charge_id": 542814 }, "created_at": "2018-12-03 11:15:24" }, 
 { "id": 2, "type": "charge", "custom_id": "1208", "status": { "current": "waiting", "previous": "new" }, "identifiers": { "charge_id": 542814 }, "created_at": "2018-12-03 11:15:28" } ] }

My goal is to get the "status" item. The following code works fine:

dynamic stuff1 = Newtonsoft.Json.JsonConvert.DeserializeObject(jo);
string Text = stuff1.data.status.current;

The code works to get the item from the first line, but how do I get the status attribute always from the last line , because it will always grow. I tried using .ToLast () to get the last record of "current" but I'm not succeeding.

    
asked by anonymous 03.12.2018 / 18:14

2 answers

1

@Antonio's answer is very well done, as it gives you a way to deserialize your JSON and turn it into an object you can use throughout your system flow.

I will give you an answer if you only need the attribute of the last line and do not want to do anything else with the other information, ie if you do not need to use the other data you can follow the following alternative.

//Temos nosso JSON salvo nessa variável
string json = "{ \"code\": 200,\"data\": [{ \"id\": 1, \"type\": \"charge\", \"custom_id\": \"1208\", \"status\": { \"current\": \"new\", \"previous\": null }, \"identifiers\": { \"charge_id\": 542814 }, \"created_at\": \"2018-12-03 11:15:24\" },  { \"id\": 2, \"type\": \"charge\", \"custom_id\": \"1208\", \"status\": { \"current\": \"waiting\", \"previous\": \"new\" }, \"identifiers\": { \"charge_id\": 542814 }, \"created_at\": \"2018-12-03 11:15:28\" } ] }";

//Criamos nosso tipo anônimo apenas definindo as propriedades e seus tipos de dados que nos interessam
var anonymousType = new
{
    data = new[]
    {
        new
        {
            status = new
            {
                        current = ""
                    }
                }
            }
        };

// Pegamos o último elemento "data" e pegamos o valor do "current" caso ele exista.
var current = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(json, anonymousType)?.data?.LastOrDefault()?.status?.current;
    
04.12.2018 / 13:11
2

First you have to create an object with the structure similar to json for JsonConvert to know where to map the objects. According to this JSON will be something of the gender

public class RootObject
{
    public string Code { get; set; }
    public IList<Data> Data { get; set; }
}

public class Data
{
    public int Id { get; set; }

    public string Type { get; set; }

    [JsonProperty(PropertyName = "custom_id")]
    public string CustomerId { get; set; }

    public Status Status { get; set; }

    public Identifiers Identifiers { get; set; }

    [JsonProperty(PropertyName = "created_at")]
    public DateTime CreatedAt { get; set; }
}

public class Status
{
    public string Current { get; set; }
    public string Previous { get; set; }
}

public class Identifiers
{
    [JsonProperty(PropertyName = "charge_id")]
    public string ChargeId { get; set; }
}

Then just do the deserialization and go get the last result:

var json = @" string com o json";
var res = JsonConvert.DeserializeObject<RootObject>(json);
var lastData = res.Data.LastOrDefault();
    
03.12.2018 / 18:34