1

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"
}

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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"}}

Comments

0

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

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.