Home > database >  Deserialize Complex/Branched JSON and accessing branches
Deserialize Complex/Branched JSON and accessing branches

Time:06-16

I have a JSON response which i generated a model from. the JSON data has various branches with data as shown below.

 {
"error": false,
"result": {
    "customers": [
        {
            "district": null,
            "feeder": "XXXXXXXXXXX",
            "feeder_code": "XXXXXXXXXXX",
            "transformer": "XXXXXXXXXXX",
            "dss_code": "XXXXXXXXX",
            "database_match": "XXXXXXXXXXX",
            "accountnumber": "XXXXXXXXXXX",
            "meterno": "XXXXXXXXXXX",
            "oldaccountnumber": "XXXXXXXXXXX",
            "odoo_account_number": "XXXXXXXXXXX",
            "customername": "XXXXXXXXXXX",
            "address": "XXXXXXXXXXX",
            "phone": "",
            "tarrif": "XXX",
            "tariff_category": "NON-MD",
            "service_band": "C  ",
            "status": "Active",
            "status_period": "MAY",
            "payment_status": null,
            "arrears": "29431.78",
            "long_x": "7.0385020000",
            "lat_y": "5.4909420000",
            "transactional_details": {
                "queryresult": {
                    "responseMessage": "success",
                    "customer": {
                        "accountNumber": "XXXXXXXXXXX",
                        "meterNumber": "XXXXXXXXXXX",
                        "phoneNumber": "",
                        "lastName": "XXXXXXXXXXX",
                        "address": "XXXXXXXXXXX",
                        "city": "XXXXXXXXXXX",
                        "district": "Owerri",
                        "userCategory": "NON-MD",
                        "customerType": "metered",
                        "paymentPlan": "Prepaid",
                        "vat": 0,
                        "tariffCode": "R2SC-NMD",
                        "tariffRate": 52.6,
                        "arrearsBalance": 0,
                        "billedAmount": 0,
                        "billedDate": null,
                        "lastPayDate": "2021-09-21 12:16:46",
                        "lastpayment": {
                            "transactionRef": "88099064",
                            "units": 0,
                            "transactionDate": "JXXXXXXXXXXX",
                            "transactionId": "XXXXXXXXXXX",
                            "transactionBookId": 0,
                            "amountPaid": 1000,
                            "mscPaid": 0,
                            "invoiceNumber": "17289583"
                        }
                    },
                    "responseCode": 200,
                    "status": "true"
                },
                "status": true
            }
        }
       
        
    ],
    "row_count": 2
}

}

I have various branches in this JSON response which i need to use. i generated a model class from this JSON as shown below

 // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Customer
{
    public object district { get; set; }
    public string feeder { get; set; }
    public string feeder_code { get; set; }
    public string transformer { get; set; }
    public string dss_code { get; set; }
    public string database_match { get; set; }
    public string accountnumber { get; set; }
    public string meterno { get; set; }
    public string oldaccountnumber { get; set; }
    public string odoo_account_number { get; set; }
    public string customername { get; set; }
    public string address { get; set; }
    public string phone { get; set; }
    public string tarrif { get; set; }
    public string tariff_category { get; set; }
    public string service_band { get; set; }
    public string status { get; set; }
    public string status_period { get; set; }
    public object payment_status { get; set; }
    public string arrears { get; set; }
    public string long_x { get; set; }
    public string lat_y { get; set; }
    public TransactionalDetails transactional_details { get; set; }
}

public class Customer2
{
    public string accountNumber { get; set; }
    public string meterNumber { get; set; }
    public string phoneNumber { get; set; }
    public string lastName { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string district { get; set; }
    public string userCategory { get; set; }
    public string customerType { get; set; }
    public string paymentPlan { get; set; }
    public int vat { get; set; }
    public string tariffCode { get; set; }
    public double tariffRate { get; set; }
    public int arrearsBalance { get; set; }
    public int billedAmount { get; set; }
    public object billedDate { get; set; }
    public string lastPayDate { get; set; }
    public Lastpayment lastpayment { get; set; }
}

public class Lastpayment
{
    public string transactionRef { get; set; }
    public int units { get; set; }
    public string transactionDate { get; set; }
    public string transactionId { get; set; }
    public int transactionBookId { get; set; }
    public int amountPaid { get; set; }
    public int mscPaid { get; set; }
    public string invoiceNumber { get; set; }
}

public class Queryresult
{
    public string responseMessage { get; set; }
    public Customer customer { get; set; }
    public int responseCode { get; set; }
    public string status { get; set; }
}

public class Result
{
    public ObservableCollection<Customer> customers { get; set; }
    public int row_count { get; set; }
}

public class Root
{
    public bool error { get; set; }
    public Result result { get; set; }
}

public class TransactionalDetails
{
    public Queryresult queryresult { get; set; }
    public bool status { get; set; }
}

Afterwards i used the C# code to deserialize the JSON as shown below

   Root myroot = JsonConvert.DeserializeObject<Root>(readTask);
                              
                                ObservableCollection<Customer> data = myroot.result.customers;

This works fine, however, i am stuck with just customer Data. I want to be able to get the Transactional details (Customer2) and Lastpayment branches in a collection view. how can i achieve this? Thanks

CodePudding user response:

Customers is an array object so you must iterate through this array.

For example this would work:

Root myroot = JsonConvert.DeserializeObject<Root>(json);
myroot.result.customers.ToList().ForEach(c =>
{
    Console.WriteLine(c.transactional_details.queryresult.customer.lastpayment.amountPaid);
});

Looking at Locals in vs debugger for var customers = myroot.result.customers.ToList(); we get this: From debugger

CodePudding user response:

You can parse your json and read it by linq. ToObject method help you to convert nodes to specific class. I have taken json text in InputJson variable and then paresed it.

var parsed = JObject.Parse(InputJson);

var innerNodes = JArray.Parse(parsed["result"]["customers"].ToString());
var CustomerList = innerNodes.Select(x => x.ToObject<Customer>()).ToList(); 
  • Related