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 aarray
, and not an object.– Wallace Maxters
@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.
– Gabriel Rio
This answers your question? How to save parts of a json in variable?
– tvdias
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 theProductDescription
– Leandro Angelo