简体   繁体   English

在 Java 中创建嵌套的 json 对象

[英]Creating nested json objects in Java

I need to create JSON like below to send with a POST request.我需要像下面一样创建 JSON 来发送 POST 请求。 So far I tried to accomplish this by using Org.Json library到目前为止,我试图通过使用 Org.Json 库来实现这一点

"Configs": {
 "TeamEvents": {
    "3050": [1,2,3,4,5,6,7,8],
    "3052": [1,2,8],
    "3054": [4]
 }

4 digit numbers are ids. 4位数字是ID。 However I could not grasp how can I do this part.但是我无法理解我该怎么做这部分。 I've created TeamEvents JSONObject and added id's and arrays with JSONObject.put() method but I dont know how can I pass that TeamEvents object to Configs.我已经使用 JSONObject.put() 方法创建了 TeamEvents JSONObject 并添加了 ID 和 arrays,但我不知道如何将 TeamEvents object 传递给 Configs。 Thanks in advance提前致谢

You've basically already found your solution, you just haven't realised it!您基本上已经找到了解决方案,只是您还没有意识到!

You can add a JSONObject to a JSONObject.您可以将 JSONObject 添加到 JSONObject。

So you can create your teamEvents JSONObject, .put("each id", value), then create a new configs JsonObject, and configsObject.put("TeamEvents", teamEventsObject).所以你可以创建你的 teamEvents JSONObject, .put("each id", value),然后创建一个新的配置 JsonObject 和 configsObject.put("TeamEvents", teamEventsObject)。

So like this...所以像这样...

JSONObject teamEventsObject.put("3050", [1,2,3,4,5,6,7,8]);
teamEventsObject.put("3052", [1,2,8]); 
... etc

JSONObject configsObject.put("Configs", teamEventsObject);

Or indeed as Enes has implied, you could do it via creation of java POJOs, then serialize those objects into json, eg.或者实际上正如 Enes 暗示的那样,您可以通过创建 java POJO 来实现,然后将这些对象序列化为 json,例如。 with the Jackson objectMapper.使用 Jackson objectMapper。 This would be a more solid 'contract' than hardcoding string JSONObjects, but both ways achieve the same result.这将是一个比硬编码字符串 JSONObjects 更可靠的“合同”,但两种方式都能达到相同的结果。

Use Gson Library .使用Gson 库

Check user guide for more complex example.查看用户指南以获取更复杂的示例。

Simple Example简单示例


class DataClass {
    private int code = 1;
    private String name = "abc";
    DataClass() {
        // no-args constructor
    }
}

// Serialization
DataClass data = new DataClass();
Gson gson = new Gson();
String json = gson.toJson(data);

// ==> json is {"code":1,"name":"abc"}
    
// Deserialization
DataClass data = gson.fromJson(json, DataClass.class);

Another way to achieve this is simply serialize object to JSON string with any popular JSON library such as Jackson, Gson and so on. Another way to achieve this is simply serialize object to JSON string with any popular JSON library such as Jackson, Gson and so on.

First, construct a nested map of objects as follows:首先,构造对象的嵌套 map,如下所示:

Map<String, Object> teamEventMap = new HashMap<>();
teamEventMap.put("3050", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
teamEventMap.put("3052", Arrays.asList(1, 2, 8));
teamEventMap.put("3054", Arrays.asList(4));

Map<String, Object> configMap = new HashMap<>();
configMap.put("TeamEvents", teamEventMap);

Map<String, Object> resultMap = new HashMap<>();
resultMap.put("Configs", configMap);

Then leverage the JSON library for serialization without directly operating JSON objects:然后利用 JSON 库进行序列化,无需直接操作 JSON 对象:

// By using Jackson
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writeValueAsString(resultMap));

// By using Gson
Gson gson = new Gson();
System.out.println(gson.toJson(resultMap));

Both two JSON libraries produce the same output as expected:两个 JSON 库都按预期生成相同的 output:

{"Configs":{"TeamEvents":{"3054":[4],"3052":[1,2,8],"3050":[1,2,3,4,5,6,7,8]}}} {"Configs":{"TeamEvents":{"3054":[4],"3052":[1,2,8],"3050":[1,2,3,4,5,6,7, 8]}}}


One of the benefits for this way resides it is easy to switch to another JSON library without much code change.这种方式的好处之一在于无需太多代码更改即可轻松切换到另一个 JSON 库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM