Java-json

json to object

import com.fasterxml.jackson.databind.ObjectMapper;// in play 2.3
ObjectMapper mapper = new ObjectMapper();

// As Object
modelResult resultDatas = objectMapper.readValue(content, modelResult.class);

//As Array:
MyClass[] myObjects = mapper.readValue(json, MyClass[].class);

//As List:
List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});

//Another way to specify the List type:
List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));

object to json

User user = new User();
user.setFirstName("Shane);
user.setLastName("James");
user.setAge(28);

String userJson = null;
try {
userJson = objectMapper.writeValueAsString(user);
} catch (JsonProcessingException e) {
e.printStackTrace();
}

map to json

ObjectMapper mapper = new ObjectMapper();
String json = "";

Map<String, String> map = new HashMap<String,String>();
map.put("name", "zitong");
map.put("age", "26");

json = mapper.writeValueAsString(map);