jar:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.42</version>
</dependency>
@Test
public void json2Obj() throws Exception{
//json2obj
String json = "{\"attr\":\"2\",\"updateTime\":\"2017-03-08 12:01:03\",\"id\":\"1\",\"product\":\"1\",\"status\":\"1\",\"unit\":\"2\"}";
String jsonArray = "[{\"attr\":\"2\",\"updateTime\":\"20170308120103\",\"id\":\"1\",\"product\":\"1\",\"status\":\"1\",\"unit\":\"2\"},{\"attr\":\"2\",\"id\":\"2\",\"product\":\"1\",\"status\":\"1\",\"unit\":\"2\"}]";
System.out.println(json);
// JSON.DEFFAULT_DATE_FORMAT = "yyyyMMddhh:MM:dd";
Product product = JSON.parseObject(json,Product.class);
List<Product> parseObject = JSON.parseArray(jsonArray,Product.class);
//obj2json
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
System.out.println(JSON.toJSONString(parseObject, SerializerFeature.WriteDateUseDateFormat));
System.out.println(0);
// result:
// {"attr":"2","updateTime":"2017-03-08 12:01:03","id":"1","product":"1","status":"1","unit":"2"}
// [{"attr":"2","id":"1","product":"1","status":"1","unit":"2","updateTime":"2017-03-08"},{"attr":"2","id":"2","product":"1","status":"1","unit":"2"}]
}
2.属性名开头大写时
private String Name_OCR;
GSON.toJson(object):结果:{"name_OCR":xx}
如果需要保持属性的名称不变,使用@JSONField
@JSONField(name="Name_OCR")
private String Name_OCR;