0

I need to create JSON like below to send with a POST request. So far I tried to accomplish this by using Org.Json library

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

4 digit numbers are ids. 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. Thanks in advance

3 Answers 3

2

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<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 just leverage JSON library for serialization without directly operating JSON objects:

// 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:

{"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.

Sign up to request clarification or add additional context in comments.

Comments

1

Use Gson Library.

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

Comments

1

You've basically already found your solution, you just haven't realised it!

You can add a JSONObject to a JSONObject.

So you can create your teamEvents JSONObject, .put("each id", value) , then create a new configs JsonObject, and 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. with the Jackson objectMapper. This would be a more solid 'contract' than hardcoding string JSONObjects, but both ways achieve the same result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.