Java map to json using Jackson example




Program

package com.JasonProjectDeomo.Jackson.JsonWrite;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.codehaus.jackson.util.DefaultPrettyPrinter;

public class JavaMapToJsonFile {
public static void main(String[] args) throws JsonGenerationException,
JsonMappingException, IOException {

Map<String, Object> person = new HashMap<String, Object>();
Map<String, String> address = new HashMap<String, String>();
address.put("Vill.", "Dhananjaypur");
address.put("Dist.", "Varanasi");
address.put("State", "UP");
person.put("id", "1");
person.put("name", "Arvind");
person.put("address", address);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
mapper.writeValue(new File("JacksonFile/dataTwo.json"), person);
System.out.println("--Done--");
}
}



Output

--Done--



Comments


©candidjava.com