原始问题:已解决
我使用JsonObject / JsonArray从控制器生成Json:
JSONObject headers = new JSONObject();
headers.put("ID", "");
headers.put("Organization Name", "");
headers.put("Submission Date", "");
headers.put("Status", "");
JSONObject organizationsHJ = new JSONObject();
organizationsHJ.put("headers", headers);
array.add(organizationsHJ );
这会产生JSON:
"headers":{"Status":"","Submission Date":"","Organization Name":"","ID":""}
相反,我需要将输出作为:
"headers":[ "ID", "Organization Name", "Submission Date", "Status" ]
这可能吗?请指教。请注意我可以将JSON作为javascript变量,如果这更容易吗?
修改
我需要在JSON输出中进行一个更简单的更改。这是我的代码:
JSONObject notifications = new JSONObject();
notifications.put("id", "1");
notifications.put("description", "Notification 1");
notifications.put("createdTimestamp", "2015-05-12T18:15:28.237Z");
notifications.put("startTimestamp", "2015-05-25T18:30:28.237Z");
notifications.put("endTimestamp", "2015-06-13T12:30:30.237Z");
notifications.put("active", "true");
这会生成JSON输出:
{"data":{"id":"1","createdTimestamp":"2015-05-12T18:15:28.237Z","description":"Notification 1","startTimestamp":"2015-05-25T18:30:28.237Z","active":"true","createdId":"251","endTimestamp":"2015-06-13T12:30:30.237Z"}}
相反,我希望它生成为:
{"data":["id":"1","createdTimestamp":"2015-05-12T18:15:28.237Z","description":"Notification 1","startTimestamp":"2015-05-25T18:30:28.237Z","active":"true","createdId":"251","endTimestamp":"2015-06-13T12:30:30.237Z"]}
答案 0 :(得分:2)
headers
应该是JSONArray
,而不是JSONObject
。对象是一组名称到值的映射,数组是线性集合。
JSONArray headers = new JSONObject();
headers.add("ID");
headers.add("Organization Name");
headers.add("Submission Date");
headers.add("Status");
JSONObject organizationsHJ = new JSONObject();
organizationsHJ.put("headers", headers);
array.add(organizationsHJ );
答案 1 :(得分:1)
organizationsHJ.put(“headers”,headers.keySet());