У меня есть вложенные jsonobjects с jsonarray, которые я должен отправить по запросу. Как я должен это делать. образец 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"
}