[英]Customise the Json in Android Java
我有这样的Json
..
我用过 Gson
[
{
"quantity": 12.0,
"product_gid": 15
},
{
"quantity": 6.0,
"product_gid": 18
}
]
我使用了 Json.put
{
"ACTION": "Insert",
"soheader_gid": 0,
"emp_gid": 59,
"custid": 120
}
现在我需要将数据自定义为
{
"parms": {
"emp_gid": 59,
"soheader_gid": 0,
"custid": "120",
"ACTION": "Insert",
"data": {
"sodetails": [
{
"quantity": 12.0,
"product_gid": 15
},
{
"quantity": 6.0,
"product_gid": 18
}
]
}
}
}
即使这在Python
或.net
很简单,我也很困惑在 Java Android 中如何做。
嗨,您可以尝试以下代码来构建您的 json,
String message;
JSONObject json = new JSONObject();
JSONObject json1 = new JSONObject();
JSONObject json2 = new JSONObject();
JSONObject json3 = new JSONObject();
JSONObject json4 = new JSONObject();
JSONArray jsonArray=new JSONArray();
json1.put("emp_gid", 59);
json1.put("soheader_gid", 0);
json1.put("custid", 120);
json1.put("ACTION", "Insert");
json1.put("data", json2);
json.put("parms", json1);
json3.put("quantity", 12.0);
json3.put("product_gid", 15);
json4.put("quantity", 6.0);
json4.put("product_gid", 18);
jsonArray.put(json3);
jsonArray.put(json4);
json2.put("sodetails", jsonArray);
message = json.toString();
@注意:上面的代码只是将两个json转换为一个的过程。
我们做到了
JSONObject Full_Json = new JSONObject();
JSONObject params_Json = new JSONObject();
params_Json.put(Constant.emp_gid, Integer.parseInt(UserDetails.getUser_id()) );
params_Json.put(Constant.soheader_gid, 0);
params_Json.put(Constant.customer_gid, cust_gid);
params_Json.put(Constant.Action, "Insert");
params_Json.put(Constant.Data, new JSONObject().put(Constant.sodetails, soDetails));
Full_Json.put(Constant.params, params_Json);
soDetails 是一个 Json 数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.