jackson json 简单入门
来源:互联网 发布:同性约会软件 编辑:程序博客网 时间:2024/05/17 08:04
本文转自:http://blog.csdn.net/nomousewch/article/details/8949899
- 概述
Jackson库(http://jackson.codehaus.org),是基于java语言的开源json格式解析工具,整个库(使用最新的2.2版本)包含3个jar包:
- jackson-core.jar——核心包(必须),提供基于“流模式”解析的API。
- jackson-databind——数据绑定包(可选),提供基于“对象绑定”和“树模型”相关API。
- jackson-annotations——注解包(可选),提供注解功能。
- Jackson的优势
相对于java json解析的其他库,诸如json-lib、gson包,Jackson具有以下优点:
- 功能全面,提供多种模式的json解析方式,“对象绑定”使用方便,利用注解包能为我们开发提供很多便利。
- 性能较高,“流模式”的解析效率超过绝大多数类似的json包。
- 重要API
- 核心包:JsonPaser(json流读取),JsonGenerator(json流输出)。
- 数据绑定包:ObjectMapper(构建树模式和对象绑定模式),JsonNode(树节点)。
- 简单例子
在实际应用中,用的最多的是数据对象绑定的模式,即将一个对象序列化为json字符串和将一串json字符串反序列化为java对象或Map。
Person类:
- public class Person {
- private String name;
- private int age;
- 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;
- }
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public Person() {
- }
- }
- public class Demo {
- public static void main(String[] args) {
- // writeJsonObject();
- // readJsonObject();
- // readJsonMap();
- }
- // 直接写入一个对象
- public static void writeJsonObject() {
- ObjectMapper mapper = new ObjectMapper();
- Person person = new Person("nomouse", 25);
- try {
- mapper.writeValue(new File("c:/person.json"), person);
- } catch (JsonGenerationException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 直接将一个json转化为对象
- public static void readJsonObject() {
- ObjectMapper mapper = new ObjectMapper();
- try {
- Person person = mapper.readValue(new File("c:/person.json"),
- Person.class);
- System.out.println(person.getName() + ":" + person.getAge());
- } catch (JsonParseException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 直接转化为map
- public static void readJsonMap() {
- ObjectMapper mapper = new ObjectMapper();
- try {
- // 需要注意的是这里的Map实际为一个LikedHashMap,即链式哈希表,可以按照读入顺序遍历
- Map map = mapper.readValue(new File("c:/person.json"), Map.class);
- System.out.println(map.get("name") + ":" + map.get("age"));
- } catch (JsonParseException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
下面介绍一下最常用的一些注解
- @JsonIgnoreProperties
此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。
- @JsonIgnore
此注解用于属性或者方法上(最好是属性上),作用和上面的@JsonIgnoreProperties一样。
- @JsonFormat
此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式,比如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")
- @JsonSerialize
此注解用于属性或者getter方法上(本人jackson-all-1.7.6.jar测试,在getter方法上使用生效),用于在序列化时嵌入我们自定义的代码,比如序列化一个double时在其后面限制两位小数点。
- public class CustomDoubleSerialize extends JsonSerializer<Double> {
- private DecimalFormat df = new DecimalFormat("##.00");
- @Override
- public void serialize(Double value, JsonGenerator jgen,
- SerializerProvider provider) throws IOException,
- JsonProcessingException {
- jgen.writeString(df.format(value));
- }
- }
- @JsonDeserialize
此注解用于属性或者setter方法上(本人jackson-all-1.7.6.jar测试,在setter方法上使用生效),用于在反序列化时可以嵌入我们自定义的代码,类似于上面的@JsonSerialize
- public class CustomDateDeserialize extends JsonDeserializer<Date> {
- private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- @Override
- public Date deserialize(JsonParser jp, DeserializationContext ctxt)
- throws IOException, JsonProcessingException {
- Date date = null;
- try {
- date = sdf.parse(jp.getText());
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return date;
- }
- }
- 完整例子
- //表示序列化时忽略的属性
- @JsonIgnoreProperties(value = { "word" })
- public class Person {
- private String name;
- private int age;
- private boolean sex;
- private Date birthday;
- private String word;
- private double salary;
- 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;
- }
- public boolean isSex() {
- return sex;
- }
- public void setSex(boolean sex) {
- this.sex = sex;
- }
- public Date getBirthday() {
- return birthday;
- }
- // 反序列化一个固定格式的Date
- @JsonDeserialize(using = CustomDateDeserialize.class)
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public String getWord() {
- return word;
- }
- public void setWord(String word) {
- this.word = word;
- }
- // 序列化指定格式的double格式
- @JsonSerialize(using = CustomDoubleSerialize.class)
- public double getSalary() {
- return salary;
- }
- public void setSalary(double salary) {
- this.salary = salary;
- }
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public Person(String name, int age, boolean sex, Date birthday,
- String word, double salary) {
- super();
- this.name = name;
- this.age = age;
- this.sex = sex;
- this.birthday = birthday;
- this.word = word;
- this.salary = salary;
- }
- public Person() {
- }
- @Override
- public String toString() {
- return "Person [name=" + name + ", age=" + age + ", sex=" + sex
- + ", birthday=" + birthday + ", word=" + word + ", salary="
- + salary + "]";
- }
- }
- public class Demo {
- public static void main(String[] args) {
- writeJsonObject();
- // readJsonObject();
- }
- // 直接写入一个对象(所谓序列化)
- public static void writeJsonObject() {
- ObjectMapper mapper = new ObjectMapper();
- Person person = new Person("nomouse", 25, true, new Date(), "程序员",
- 2500.0);
- try {
- mapper.writeValue(new File("c:/person.json"), person);
- } catch (JsonGenerationException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 直接将一个json转化为对象(所谓反序列化)
- public static void readJsonObject() {
- ObjectMapper mapper = new ObjectMapper();
- try {
- Person person = mapper.readValue(new File("c:/person.json"),
- Person.class);
- System.out.println(person.toString());
- } catch (JsonParseException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- jackson json 简单入门
- 【JackSon Json】 入门1
- Json解析工具Jackson(简单应用)
- Json解析工具Jackson(简单应用)
- Json解析工具Jackson(简单应用)
- Json解析工具Jackson(简单应用)
- Android JackSon (Json工具) 简单使用封装
- Json解析工具Jackson(简单应用)
- Json解析工具Jackson(简单应用)
- Json解析工具Jackson(简单应用)
- Json解析工具Jackson(简单应用)
- Json解析工具Jackson(简单应用)
- Json解析工具Jackson(简单应用)
- Json解析工具Jackson(简单应用)
- Jackson JSON
- json-> jackson
- jackson 入门
- JackSon入门
- 采用aidl访问远程服务里面的方法
- Ubuntu下无法启动Eclipse的原因
- Qt下的跨目录多工程编译
- C#中的并行与多线程编程
- Linux0.12内核读书笔记
- jackson json 简单入门
- Html5游戏开发攻略(API篇)
- Ubuntu12.04搭建Hadoop环境
- qmake使用实践:包含动态库的Qt4工程
- 我的第一课:jquery实现点击消失
- 在android中使用SOAP与webservice进行数据交互
- DataTable转换为List<Model>的通用类(绑定Repeater智能提示)
- S3C2440 clock研究
- 排他平方数