Java 中的 JsonObject
JsonObject in Java
原问题:已解决
我正在使用 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"]}
organizationsHJ.put("headers", headers.keySet());
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 );
原问题:已解决
我正在使用 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"]}
organizationsHJ.put("headers", headers.keySet());
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 );