-1

I'm having a hash map with set of keys and values.

I would like to convert it to a json format and print the entire string.

I don't like to create a file and need to dynamically print the string in the screen. i'm using fasterxml api. (http://wiki.fasterxml.com/JacksonInFiveMinutes)

Please let me know how to do it.

Serenity
  • 35,289
  • 20
  • 120
  • 115
Kathir J
  • 1
  • 3

1 Answers1

0

Please look at the following:

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

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

public class Test { 

public static void main(String[] args) throws JsonProcessingException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put( "language", "Java" );
    map.put( "year", 2016 );
    map.put( "isObjectOriented", true );        
    ObjectMapper mapper = new ObjectMapper();
    String jsonInString = mapper.writeValueAsString(map);      
    System.out.printf( "JSON: %s", jsonInString );     
}   

}

I have used following jars: jackson-core-2.7.4.jar, jackson-databind-2.7.4.jar and jackson-annotations-2.7.4.jar. Hope it helps.

Sanjeev Saha
  • 2,632
  • 1
  • 12
  • 19