package com.json.demo3_26;
import java.util.LinkedHashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class MapToJson {
public static void main(String[] args) throws JsonProcessingException {
Map<String,Object> mapJson = new LinkedHashMap<>();
mapJson.put("key1","value1");
mapJson.put("key2","value2");
mapJson.put("key3","value3");
mapJson.put("booleanTrue",true);
mapJson.put("booleanFalse",false);
mapJson.put("Integer One",1);
mapJson.put("Integer Two",2);
mapJson.put("Object",new Person(1,"Tyson",60));
mapJson.put("NullValue",null);
mapJson.put("Array",new String[] {"One","Two","Three"});
mapJson.put("ArrayOfObjects",new Person[] {new Person(1,"Tyson",60),new Person(2,"Justin",70)});
System.out.println(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(mapJson));
}
}
class Person {
@JsonProperty(value = "Person ID")
int id;
@JsonProperty(value = "Person Name")
String name;
@JsonProperty(value = "Person Age")
int age;
Person(int id,String name,int age){
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}