Convert JSON file to object - C# Xamarin

Asked

Viewed 78 times

1

I have the following JSON file:

{  
   "createdAt":"Apr 17, 2019 10:28:39",
   "id":"52329770-64e2-4759-9d3d-f2595973de4e",
   "items":[  
      {  
         "id":"82d2e4ab-da74-45df-a9ea-99cc257ead59",
         "name":"REFRIGERANTE KS",
         "quantity":1,
         "sku":"20",
         "unitOfMeasure":"un",
         "unitPrice":1440
      }
   ],
   "notes":"MESA",
   "paidAmount":1440,
   "payments":[  
      {  
         "accessKey":"0gyZYCs8kJIucvOCdc7dsNvOehcsRXTRX2iGNFUr7hdc1sUWx5",
         "amount":1440,
         "applicationName":"CieloLIO",
         "paymentFields":{  
            "clientName":"CieloLio",
            "upFrontAmount":"1000",
            "creditAdminTax":"500",
            "hasSignature":"true",
            "hasPrintedClientReceipt":"true",
            "primaryProductName":"DEBITO",
            "reference":"112121"
         },
         "primaryCode":"8",
         "secondaryCode":"208",
         "terminal":"mock_terminal"
      }
   ],
   "pendingAmount":0,
   "price":1440,
   "reference":"284807",
   "status":"PAID",
   "type":"PAYMENT",
   "updatedAt":"Apr 17, 2019 10:29:06"
}

How do I turn this into an object? Do I have to have a class with all these properties? What function do I use to transform into an object?

  • 2

    Give a web-searched how to install Newtonsoft nuget.Json, he does it! You will only need to create the model to store these values and call a function from it...

  • 1

    I make the recommendation table of @Lodi. Use the Json.NET

  • 1

    You will need all the information or just some data?

  • At first I won’t need all the information. I managed to solve using Newtonsoft.Json, thanks!

1 answer

1


SOLUTION

First I had to create the classes to store JSON information. I found a website who does all this work for me:

public class Item
{
    public string id { get; set; }
    public string name { get; set; }
    public int quantity { get; set; }
    public string sku { get; set; }
    public string unitOfMeasure { get; set; }
    public int unitPrice { get; set; }
}

public class PaymentFields
{
    public string clientName { get; set; }
    public string upFrontAmount { get; set; }
    public string creditAdminTax { get; set; }
    public string hasSignature { get; set; }
    public string hasPrintedClientReceipt { get; set; }
    public string primaryProductName { get; set; }
    public string reference { get; set; }
}

public class Payment
{
    public string accessKey { get; set; }
    public int amount { get; set; }
    public string applicationName { get; set; }
    public PaymentFields paymentFields { get; set; }
    public string primaryCode { get; set; }
    public string secondaryCode { get; set; }
    public string terminal { get; set; }
}

public class RootObject
{
    public string createdAt { get; set; }
    public string id { get; set; }
    public List<Item> items { get; set; }
    public string notes { get; set; }
    public int paidAmount { get; set; }
    public List<Payment> payments { get; set; }
    public int pendingAmount { get; set; }
    public int price { get; set; }
    public string reference { get; set; }
    public string status { get; set; }
    public string type { get; set; }
    public string updatedAt { get; set; }
}

After that, just use the newtonsof to deserialize the object:

RootObject m = JsonConvert.DeserializeObject<RootObject>(json);

And access the properties like this:

string status = m.Status;

Browser other questions tagged

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