嗨我是这个论坛的新手我在Android工作室尝试以下内容,我需要用这种格式创建一个json
对象
{
"enviaya_account": "Y0DCRGIU",
"carrier_account": null,
"api_key":"YOUR_API_KEY",
"shipment":{
"shipment_type":"Package",
"parcels":[
{
"quantity":"1",
"weight":"3",
"weight_unit":"kg",
"length":"10",
"height":"20",
"width":"30",
"dimension_unit":"cm"
}
]
},
"origin_direction":{
"country_code":"MX",
"postal_code":"11550"
},
"destination_direction":{
"country_code":"MX",
"postal_code":"01210"
},
"insured_value":"5000",
}
"insured_value_currency":"MXN"
}
但是我无法很好地整合它,因为它标志着我的错误可以有人帮助我吗?我的代码就是:
JSONObject Request = new JSONObject();
JSONObject shipment = new JSONObject();
JSONArray parcels = new JSONArray();
JSONObject parcel= new JSONObject();
JSONObject origin_direction = new JSONObject();
JSONObject destination_direction = new JSONObject();
try {
Request.put("enviaya_account","Y0DCRGIU");
Request.put("carrier_account","");
Request.put("api_key","xxxxxxxxxxxxxxxxxxxxxxxxxxx");
shipment.put("shipment_type","package");
Request.put("shipment",shipment);
parcel.put("quantity","1");
parcel.put("weight","10.98");
parcel.put("weight_unit","kg");
parcel.put("length","39");
parcel.put("height","39");
parcel.put("width","29");
parcel.put("dimension_unit","cm");
parcels.put(parcel);
Request.put("parcels",parcels);
origin_direction.put("country_code","MX");
origin_direction.put("postal_code","29267");
destination_direction.put("country_code","MX");
destination_direction.put("postal_code","34200");
Request.put("origin_direction",origin_direction);
Request.put("destination_direction",destination_direction);
Request.put("insured_value","0");
Request.put("insured_value_currency","MXN");
} catch (JSONException e) {
e.printStackTrace();
}
edt.setText(Request.toString());
但是当我尝试在POSTMAN中检查它时,它告诉我parcels对象不存在
答案 0 :(得分:0)
据我所知,因为“parcels”被定义为JSONArray对象,所以需要调用add方法。
parcels.put(parcel);
// should instead be
parcels.add(parcel);
答案 1 :(得分:0)
我已经更改了某些部分的代码。
JSONObject Request = new JSONObject();
JSONObject shipment = new JSONObject();
JSONArray parcels = new JSONArray();
JSONObject parcel= new JSONObject();
JSONObject origin_direction = new JSONObject();
JSONObject destination_direction = new JSONObject();
try {
Request.put("enviaya_account","Y0DCRGIU");
Request.put("carrier_account","");
Request.put("api_key","xxxxxxxxxxxxxxxxxxxxxxxxxxx");
shipment.put("shipment_type","package");
parcel.put("quantity","1");
parcel.put("weight","10.98");
parcel.put("weight_unit","kg");
parcel.put("length","39");
parcel.put("height","39");
parcel.put("width","29");
parcel.put("dimension_unit","cm");
parcels.put(parcel);
shipment.put("parcels",parcels);
Request.put("shipment",shipment);
origin_direction.put("country_code","MX");
origin_direction.put("postal_code","29267");
destination_direction.put("country_code","MX");
destination_direction.put("postal_code","34200");
Request.put("origin_direction",origin_direction);
Request.put("destination_direction",destination_direction);
Request.put("insured_value","0");
Request.put("insured_value_currency","MXN");
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("Json value", String.valueOf(Request));