gpt4 book ai didi

java - Java 中的 Json 对象

转载 作者:行者123 更新时间:2023-12-01 22:18:06 24 4
gpt4 key购买 nike

原始问题:已解决

我使用 JsonObject/JsonArray 从 Controller 生成 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":{"状态":"","提交日期":"","组织名称":"","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"]}

最佳答案

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 );

关于java - Java 中的 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30583370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com