我有一个带有jsonarray的嵌套jsonobject,我必须向它发布齐射请求。我应该怎么做。下面的示例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"
}
上面是我想发送到截击请求的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"
}
以上是我想发送到截击请求的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"
}
答案 0 :(得分:0)
您要在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);