无法使用 JSONObject 正确构建 JSON 对象
Not able to build a JSON object correctly using JSONObject
我有以下程序正在构建 JSON 对象。不确定如何使用以下程序构建数组数组。
pom.xml
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
JsonObjectConverter.java
public class JsonObjectConverter {
private static final String STORE_ID = "TMSUS";
public static void main(String[] args) {
System.out.println(print1());
}
private static String print1() {
JSONObject body = new JSONObject();
JSONArray events1 = new JSONArray();
events1.add(100L);
events1.add(200L);
JSONArray events2 = new JSONArray();
events2.add(300L);
events2.add(400L);
JSONArray eventLogs = new JSONArray();
eventLogs.add(events1);
eventLogs.add(events2);
body.put("storeId", STORE_ID);
body.put("eventLogs", eventLogs);
return body.toString();
}
}
用当前程序输出:
{
"eventLogs": [
[
100,
200
],
[
300,
400
]
],
"storeId": "TMSUS"
}
预期输出:
{
"eventLogs": [
{
"storeId": "TMSUS",
"eventIds": [
100,
200
]
},
{
"storeId": "TMSCA",
"eventIds": [
300,
400
]
}
],
"userName": "meOnly"
}
不确定如何获得预期的输出。
请指导。
没关系,我让它工作了。这是更新后的方法。
private static String print1() {
JSONObject body = new JSONObject();
JSONObject eventLog1 = new JSONObject();
JSONArray events1 = new JSONArray();
events1.add(100L);
events1.add(200L);
eventLog1.put("storeId", "TMSUS");
eventLog1.put("eventIds", events1);
JSONObject eventLog2 = new JSONObject();
JSONArray events2 = new JSONArray();
events2.add(300L);
events2.add(400L);
eventLog2.put("storeId", "CBKUS");
eventLog2.put("eventIds", events2);
JSONArray eventLogs = new JSONArray();
eventLogs.add(eventLog1);
eventLogs.add(eventLog2);
body.put("eventLogs", eventLogs);
body.put("userName", "customer-portal-user");
return body.toString();
}
我有以下程序正在构建 JSON 对象。不确定如何使用以下程序构建数组数组。
pom.xml
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
JsonObjectConverter.java
public class JsonObjectConverter {
private static final String STORE_ID = "TMSUS";
public static void main(String[] args) {
System.out.println(print1());
}
private static String print1() {
JSONObject body = new JSONObject();
JSONArray events1 = new JSONArray();
events1.add(100L);
events1.add(200L);
JSONArray events2 = new JSONArray();
events2.add(300L);
events2.add(400L);
JSONArray eventLogs = new JSONArray();
eventLogs.add(events1);
eventLogs.add(events2);
body.put("storeId", STORE_ID);
body.put("eventLogs", eventLogs);
return body.toString();
}
}
用当前程序输出:
{
"eventLogs": [
[
100,
200
],
[
300,
400
]
],
"storeId": "TMSUS"
}
预期输出:
{
"eventLogs": [
{
"storeId": "TMSUS",
"eventIds": [
100,
200
]
},
{
"storeId": "TMSCA",
"eventIds": [
300,
400
]
}
],
"userName": "meOnly"
}
不确定如何获得预期的输出。
请指导。
没关系,我让它工作了。这是更新后的方法。
private static String print1() {
JSONObject body = new JSONObject();
JSONObject eventLog1 = new JSONObject();
JSONArray events1 = new JSONArray();
events1.add(100L);
events1.add(200L);
eventLog1.put("storeId", "TMSUS");
eventLog1.put("eventIds", events1);
JSONObject eventLog2 = new JSONObject();
JSONArray events2 = new JSONArray();
events2.add(300L);
events2.add(400L);
eventLog2.put("storeId", "CBKUS");
eventLog2.put("eventIds", events2);
JSONArray eventLogs = new JSONArray();
eventLogs.add(eventLog1);
eventLogs.add(eventLog2);
body.put("eventLogs", eventLogs);
body.put("userName", "customer-portal-user");
return body.toString();
}