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" }
]
jiveturkey
  • 2,484
  • 1
  • 23
  • 41
  • try the solution provided on this link. It should work : https://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java – samnit Feb 21 '18 at 14:13
  • Yes, but this doesn't get me the "address", "domain" field assignments. – jiveturkey Feb 21 '18 at 14:29

3 Answers3

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>
AxelH
  • 14,325
  • 2
  • 25
  • 55
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)));
mzherdev
  • 462
  • 1
  • 4
  • 11
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, ...

}
Mickael
  • 4,458
  • 2
  • 28
  • 40