how do i handle nested json objects in android : I have a nested jsonobjects with jsonarray which I have to post it a volley request. how should i do it. sample json below.

{
    "type":"invoice",
    "customer":{
      "name": "Deepak",
        "email":"test@test.com",
        "contact":"912345678",
        "billing_address":{
            "line1":"Bangalore",
            "city":"Bangalore",
            "state":"Karnataka",
            "zipcode":"000000",
            "country":"India"
        }
    },
    "line_items":[
        {
            "name":"News Paper",
            "description":"Times of India",
            "amount":10000,
            "currency":"INR",
            "quantity":1

        },
        {
            "name":"News Paper",
            "description":"Bangalore Mirror",
            "amount":10000,
            "currency":"INR",
            "quantity":1

        }

    ],
    "currency":"INR",
    "sms_notify": "1",
    "email_notify": "1"
}

The above is the jsonobject structure i want to send to volley request.

This is what i have done but not getting the right jsonobject.

    try {
        objMainList = new JSONObject();
        objMainList.put("type","invoice");
        headobj = new JSONObject();
        detobj = new JSONObject();
        addrobj = new JSONObject();
        footobj = new JSONObject();
        headobj.put("name", custname);
        headobj.put("email", custemail);
        headobj.put("contact", "1234567");
        addrobj.put("line1",custaddr);
        addrobj.put("city",custcity);
        addrobj.put("state",custstate);
        addrobj.put("zipcode",pincode);
        addrobj.put("country",country);
        objMainList.put("customer",headobj);
        objMainList.put("billing_address",headobj);
        JSONArray prodarray = new JSONArray();
        for (int i = 0; i < pvt_list_prodlist.size(); i++) {
            JSONObject detobj = new JSONObject();
            detobj.put("name", pvt_list_prodlist.get(i).getProductcatg());
            detobj.put("description", pvt_list_prodlist.get(i).getProductname());
            Float  total = Float.parseFloat(pvt_list_prodlist.get(i).getProductprice());
            Integer gtotal = (int)Math.ceil(total);
            gtotal = gtotal * 100;
            detobj.put("amount",gtotal );
            detobj.put("currency", "INR");
            detobj.put("quantity", 1);
            prodarray.put(detobj);
        }
        objMainList.put("line_items",prodarray);
        objMainList.put("currency","INR");
        objMainList.put("sms_notify",1);
        objMainList.put("email_notify",1);


    } catch (JSONException e) {
        // JSON error
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
    }

This is what I am getting from the above code...

{"type":"invoice","customer":{"name":"Deepak","email":"test_test.com","contact":"1234567"},"billing_address":{"line1":"Bangalore","city":"Bangalore","state":"Karnataka","zipcode":"000001","country":"India"},"line_items":[{"name":"NEWS","description":"Times of India","amount":500,"currency":"INR","quantity":1}],"currency":"INR","sms_notify":1,"email_notify":1}

before billing_address it is getting closed. I want it in the above mentioned format.




{
    "type":"invoice",
    "customer":{
      "name": "Deepak",
        "email":"test@test.com",
        "contact":"912345678",
        "billing_address":{
            "line1":"Bangalore",
            "city":"Bangalore",
            "state":"Karnataka",
            "zipcode":"000000",
            "country":"India"
        }
    },
    "line_items":[
        {
            "name":"News Paper",
            "description":"Times of India",
            "amount":10000,
            "currency":"INR",
            "quantity":1

        },
        {
            "name":"News Paper",
            "description":"Bangalore Mirror",
            "amount":10000,
            "currency":"INR",
            "quantity":1

        }

    ],
    "currency":"INR",
    "sms_notify": "1",
    "email_notify": "1"
}


The above is the jsonobject structure i want to send to volley request.

This is what i have done but not getting the right jsonobject.

    try {
        objMainList = new JSONObject();
        objMainList.put("type","invoice");
        headobj = new JSONObject();
        detobj = new JSONObject();
        addrobj = new JSONObject();
        footobj = new JSONObject();
        headobj.put("name", custname);
        headobj.put("email", custemail);
        headobj.put("contact", "1234567");
        addrobj.put("line1",custaddr);
        addrobj.put("city",custcity);
        addrobj.put("state",custstate);
        addrobj.put("zipcode",pincode);
        addrobj.put("country",country);
        objMainList.put("customer",headobj);
        objMainList.put("billing_address",headobj);
        JSONArray prodarray = new JSONArray();
        for (int i = 0; i < pvt_list_prodlist.size(); i++) {
            JSONObject detobj = new JSONObject();
            detobj.put("name", pvt_list_prodlist.get(i).getProductcatg());
            detobj.put("description", pvt_list_prodlist.get(i).getProductname());
            Float  total = Float.parseFloat(pvt_list_prodlist.get(i).getProductprice());
            Integer gtotal = (int)Math.ceil(total);
            gtotal = gtotal * 100;
            detobj.put("amount",gtotal );
            detobj.put("currency", "INR");
            detobj.put("quantity", 1);
            prodarray.put(detobj);
        }
        objMainList.put("line_items",prodarray);
        objMainList.put("currency","INR");
        objMainList.put("sms_notify",1);
        objMainList.put("email_notify",1);


    } catch (JSONException e) {
        // JSON error
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
    }

This is what I am getting from the above code...

{"type":"invoice","customer":{"name":"Deepak","email":"test_test.com","contact":"1234567"},"billing_address":{"line1":"Bangalore","city":"Bangalore","state":"Karnataka","zipcode":"000001","country":"India"},"line_items":[{"name":"NEWS","description":"Times of India","amount":500,"currency":"INR","quantity":1}],"currency":"INR","sms_notify":1,"email_notify":1}

before billing_address it is getting closed. I want it in the above mentioned format.




this is the output i should get as jsonobject .


{
    "type":"invoice",
    "customer":{
      "name": "Deepak",
        "email":"test@test.com",
        "contact":"912345678",
        "billing_address":{
            "line1":"Bangalore",
            "city":"Bangalore",
            "state":"Karnataka",
            "zipcode":"000000",
            "country":"India"
        }
    },
    "line_items":[
        {
            "name":"News Paper",
            "description":"Times of India",
            "amount":10000,
            "currency":"INR",
            "quantity":1

        },
        {
            "name":"News Paper",
            "description":"Bangalore Mirror",
            "amount":10000,
            "currency":"INR",
            "quantity":1

        }

    ],
    "currency":"INR",
    "sms_notify": "1",
    "email_notify": "1"
}