简体   繁体   English

我如何处理嵌套的 json 对象?

[英]how do i handle nested json objects?

I have a nested jsonobjects with jsonarray which I have to post it a volley request.我有一个带有 jsonarray 的嵌套 jsonobjects,我必须将它发布一个 volley 请求。 how should i do it.我该怎么做。 sample json below.下面的示例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"
}

The above is the jsonobject structure i want to send to volley request.以上是我想发送到 volley 请求的 jsonobject 结构。

This is what i have done but not getting the right 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();
    }

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.在 billing_address 之前它正在关闭。 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.以上是我想发送到 volley 请求的 jsonobject 结构。 This is what i have done but not getting the right 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();
    }

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.在 billing_address 之前它正在关闭。 I want it in the above mentioned format.我想要上面提到的格式。

this is the output i should get as jsonobject .这是我应该作为 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"
}

you are adding billing_address in main_list, it should be add in customer您正在 main_list 中添加 billing_address,它应该添加到客户中

try this尝试这个

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.

相关问题 如何使用Gson Library对嵌套的JSON对象进行排序? - How do i sort Nested JSON Objects using Gson Library? 如何使用Gson将嵌套的json对象添加到文件? - How do I add nested json Objects to file using Gson? 如何使用翻新和gson在android中处理嵌套的json对象/数组? - How to handle nested json objects/arrays in android using retrofit and gson? 如何通过pojo类从改造响应中检索嵌套在json数组中的json对象 - How do I retrieve json objects nested in a json array from a retrofit response through my pojo class 如何修改我的函数以将json对象检索到嵌套的json? - How do I modify my function to retrieve json objects to a nested json? 如何处理Runnable生成的对象? - How do I handle objects generated by a Runnable? 使用Gson优雅地处理嵌套的json对象? - Using Gson to elegantly handle nested json objects? 如何使用Retrofit 2解析嵌套对象? - How do I parse nested objects with Retrofit 2? 如何独立于嵌套的 JSON 对象检索数据,这些对象属于 android/java 中的同一组? (身体更清晰/细节) - How do I retrieve data independently from nested JSON objects that are part of the same group in android/java? ( more clarity/details in body) 如何使用翻新功能处理Flat JSON字典 - How do I handle a Flat JSON Dictionary with Retrofit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM