0

I have the following Java Map. Map<String, String> containing values such as:

876234876, google
mike@hotmail, hotmail
9879892, google

I need to convert it to the following JSON object structure and Java JSON objects are not my friend.

"addresses" : [
    { "address":"876234876", "domain":"google" },
    { "address":"mike@hotmail", "domain":"hotmail" },
    { "address":"9879892", "domain":"google" }
]
2
  • try the solution provided on this link. It should work : stackoverflow.com/questions/12155800/… Commented Feb 21, 2018 at 14:13
  • Yes, but this doesn't get me the "address", "domain" field assignments. Commented Feb 21, 2018 at 14:29

3 Answers 3

2

To create the JSON you ask, you need to insert a JSONObject into a JSONArray. So for each Entry of your Map<String, String>, create a JSONObject like {"address": entry.key, "domain": entry.value} and add those to a JSONArray.

Let's use a Stream.map to create that object and insert the result into the array directly:

public static JSONObject createJson(Map<String, String> value) {
    JSONObject result = new JSONObject();
    JSONArray addresses = new JSONArray();
    result.put("addresses", addresses);

    value.entrySet().stream()       //iterate the map
        .map(e -> {                 //build an object
            JSONObject address = new JSONObject();
            address.put("address", e.getKey());
            address.put("domain", e.getValue());
            return address;
        })
        .forEach(addresses::put);   //insert into the array

    return result;
}

And test it with :

public static void main(String[] args) {
    Map<String, String> values = new HashMap<>();
    values.put("876234876", "google");
    values.put("mike@hotmail", "hotmail");
    values.put("9879892", "google");
    System.out.println(createJson(values).toString(4));
}

And the result :

{"addresses": [
    {
        "address": "9879892",
        "domain": "google"
    },
    {
        "address": "876234876",
        "domain": "google"
    },
    {
        "address": "mike@hotmail",
        "domain": "hotmail"
    }
]}

Using the API : JSON In Java

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180130</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

1

Check this solution:

List<String> addresses = new ArrayList<>();
        map.forEach((k, v) -> {
            Map<String, String> nn = new HashMap<>();
            nn.put("address", k);
            nn.put("domain", v);
            addresses.add(JSONValue.toJSONString(nn));
        });
        JSONObject result = new JSONObject(Collections.singletonMap("addresses", new JSONArray(addresses)));

1 Comment

Awesome, solution. Had to test it first.
0

Your JAVA objects should look like this :

List of addresses

@XmlRootElement
public class Foo {

  private List<Address> addresses;

  // Getter, Setter, ...

}

Address

public class Address {

  private String address;

  private String domain;

  // Getters, setters, ...

}

1 Comment

Nice answer. I was kind of need something a little more concise - method based.

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.