簡體   English   中英

我如何處理嵌套的 json 對象?

[英]how do i handle nested json objects?

我有一個帶有 jsonarray 的嵌套 jsonobjects,我必須將它發布一個 volley 請求。 我該怎么做。 下面的示例json。

{
    "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"
}

以上是我想發送到 volley 請求的 jsonobject 結構。

這就是我所做的,但沒有得到正確的 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();
    }

這是我從上面的代碼中得到的......

{"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}

在 billing_address 之前它正在關閉。 我想要上面提到的格式。

{
    "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"
}

以上是我想發送到 volley 請求的 jsonobject 結構。 這就是我所做的,但沒有得到正確的 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();
    }

這是我從上面的代碼中得到的......

{"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}

在 billing_address 之前它正在關閉。 我想要上面提到的格式。

這是我應該作為 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"
}

您正在 main_list 中添加 billing_address,它應該添加到客戶中

嘗試這個

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);

    headobj.put("billing_address",addrobj);

    objMainList.put("customer",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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM