rajkumar.11
rajkumar.11

Reputation: 323

how to convert Map<String,TreeMap<String,String>> String into Json Object in java?

After retrieving from cache which is stored in below format

{address={address1=string, address2=string, addressId=2045, city=string, fxGeocode=string, 
houseNumber=string, isActive=true, postalCode=string, state=string, streetName=string, zip=string}, 
externalIds=[{externalId=string, externalIdDocId=38915437-69d7-449a-9bd8-9832c78b2010, 
partyRoleExternalIdType=string}], name=string, organizationRoleId=990}

i have to convert it back into json Object. What will be the best way to do it?

Converted json object is in below format

{
"address": {
    "address1": "string",
    "address2": "string",
    "addressId": "2045",
    "city": "string",
    "fxGeocode": "string",
    "houseNumber": "string",
    "isActive": "true",
    "postalCode": "string",
    "state": "string",
    "streetName": "string",
    "zip": "string"
},
"externalIds": [
    {
        "externalId": "string",
        "externalIdDocId": "38915437-69d7-449a-9bd8-9832c78b2010",
        "partyRoleExternalIdType": "string"
    }
],
"name": "string",
"organizationRoleId": "990"
}

Upvotes: 1

Views: 982

Answers (4)

Max Urbanowicz
Max Urbanowicz

Reputation: 115

Gson is a good library by Google for json serialization / deserialization. Maven dependency:

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.6</version>
</dependency>

Sample serialization:

Gson gson = new Gson();

Map<String, Map<String, String>> someMap = new HashMap<>();

try (FileWriter writer = new FileWriter("C:\\folder\\file.json")) {
    gson.toJson(someMap, writer);
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 0

Eric Green
Eric Green

Reputation: 1293

I'd do it with the Jackson JSON library.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.10.1</version>
</dependency>

This should give you the output you're looking for:

ObjectMapper mapper = new ObjectMapper();

Map<String, Map<String, String>> myMap = new HashMap<>();

// add stuff to your map of maps

// pretty print
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(myMap);

System.out.println(json);

Upvotes: 1

Navin Gelot
Navin Gelot

Reputation: 1334

You can use ObjectMapper class to turn java class object into jason and json to java class object

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Test {
    public static void main(String[] args) throws JsonProcessingException {
        Map<String, TreeMap<String,String>> m = new HashMap<>();
        TreeMap<String,String> t = new TreeMap<>();
        t.put("address1", "string");
        t.put("address2", "string");
        m.put("address", t);

        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(m);
        System.out.println(json);
    }
}

output

{"address":{"address1":"string","address2":"string"}}

Upvotes: 0

Arpit Asati
Arpit Asati

Reputation: 140

You can use com.fasterxml.jackson.core library.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.5</version>
</dependency>

     and use objectMapper to convert this object into JSON.
     //Object to JSON in String

       ObjectMapper mapper = new ObjectMapper();
      String jsonInString = mapper.writeValueAsString(object);

      // beautify JSON
      String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);

Upvotes: 0

Related Questions