在这段代码中,我试图创建类似这样的东西-
public String KLYA_JSON_LookUp_MultiNode(String KLYA_To,String KLYA_DLRURL,String KLYA_To2) {
JSONObject jsonObj = new JSONObject();
jsonObj.put("dlrurl", KLYA_DLRURL);
JSONArray array = new JSONArray();
JSONObject Array_item = new JSONObject();
Array_item.put("to", KLYA_To);
Array_item.put("to", KLYA_To2);
array.add(Array_item);
jsonObj.put("lookup", array);
CreatedJson = jsonObj.toString();
System.out.println(CreatedJson);
return CreatedJson ;
}
输出:
{"lookup": [{
"to": "8904647785"
}, {
"to": "8904647785"
}], "dlrurl": "http://www.example.com/dlr.php/......"
}
但是我没有得到上面的评论,它最终只能在数组中打印一个to
,而应该打印两个。
答案 0 :(得分:0)
插入
array.add(Array_item);
Array_item = new JSONObject();
两次调用Array_item.put
之间。
答案 1 :(得分:0)
因此,在Thomas和Petter的建议之后,我进行了研究,这就是解决问题的方法。
JSONObject jsonObj = new JSONObject();
JSONArray array = new JSONArray();
JSONObject Array_item = new JSONObject();
JSONObject NextArray_item = new JSONObject();
jsonObj.put("dlrurl", "url");
NextArray_item.put("to","XXXX");
Array_item.put("to", "XXX");
array.add(Array_item);
array.add(NextArray_item);
jsonObj.put("lookup", array);
CreatedJson = jsonObj.toString();
System.out.println(CreatedJson);