How do I get value from an internal object in a Json file?

Asked

Viewed 89 times

1

I would like to know how to access an object inside the json, follow the code:

{
  "data": [
    {
      "id": "or_asdasdasdasd",
      "code": "5d6f4sa56df",
      "amount": 800,
      "currency": "BRL",
      "closed": true,
      "items": [
        {
          "id": "oi_dasdasdasd",
          "type": "product",
          "description": "Renovação do APP",
          "amount": 200,
          "quantity": 4,
          "status": "active",
          "created_at": "2021-01-19T14:17:05Z",
          "updated_at": "2021-01-19T14:17:05Z",
          "code": "subscription"
        }
      ],

This Json is returned from an API I am consulting, not complete, just to pass the idea even.
I can access the first 5 fields and their values, but I can’t for example access the List items. follow my code:

public static void SaveOrder(IRestResponse<Root> res)
        {
            OrderDbContext bd = new OrderDbContext();
            Order order = new Order();
            

            try
            {
                order.Id = Guid.NewGuid();
                order.ExternalCodeId = res.Data.Code;
                order.ExternalCode = res.Data.Code;
                order.Currency = res.Data.Currency;
                order.ExternalProductCode = res.Data.Code;
                order.ProductDescription = res.Data.;     <<<<<<<<<<<<<<<<<<<<<
                order.Amount = res.Data.Amount;
                //order.Quantity = res.Data.;
                order.ProductStatus = res.Data.Status;
                order.ProductCode = res.Data.Code;
                order.CustomerId = Guid.NewGuid();
                order.Status = res.Data.Status;
                order.CreatedAtLocal = res.Data.Created_at;
                order.UpdatedAtUTC = res.Data.Updated_at;
                order.ClosedAt = res.Data.Closed_at;
                order.ChargeId = Guid.NewGuid();

                bd.Order.Add(order);
                bd.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

I marked with an arrow where I can’t access the fields. This method saves the information in the database.

  • Is your json like that? Why "data": [ would be a array, and not an object.

  • @Wallacemaxters Yes, json is exactly like that, within that Date is all Orderid. That Order is only one, and half. Everything inside the Data object I can access, the ones that are most encapsulated I can’t.

  • 2

    This answers your question? How to save parts of a json in variable?

  • Your code does not match the JSON presented unless you have a previous interaction or are passing only one position to SaveOrder()... And you should use the same strategy for the ProductDescription

1 answer

1

If I understand correctly, you need to access one array within an array, something like:

if (order.Data[0].Items.Length > 0) {

order.ProductDescription = res.Data.FirstOrDefault().Items.FirstOrDefault()?.Description;

or

order.ProductDescription = res.Data[0].Items[0].Description;

}

I imagine it is something like this given to the JSON you put as an example, however much you have pointed out that the way you are accessing the properties is not "matching" with the document. Your JSON has "date" as an array, so your first access to a property within the "date" property would be:

order.Data.FirstOrDefault().Code

or

order.Data[0].Code

The "Data" and "Items" properties should be lists or arrays according to their JSON, so they should be accessed as a list/array.

I hope I’ve helped in some way.

Note: it is necessary to import the namespace System.Linq to use the extension method FirstOrDefault()

Browser other questions tagged

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