//json字符串转为java对象
public static <T> T stringToObject(final Class<T> type,
String jsonString) {
T data = null;
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
data = mapper.readValue(jsonString, type);
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
//对象转为json字符串
public static String objectToJson(Object object) {
ObjectMapper m = new ObjectMapper();
m.configure(SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false);
ObjectWriter ow = m.writer().withDefaultPrettyPrinter();
String message = "";
try {
message = ow.writeValueAsString(object);
} catch (IOException e) {
Log.i("", "activity to json failure");
}
return message;
}
dependencies {
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.12'
}