jackson 入门教程

json 的基础知识。

  1. array and list –> wrap in [ ]
  2. object –> wrap in { }

jackson 需要注意

  1. 需要序列化的pojo 的属性必须具有setter和getter方法,或者是public的。

java pojo –> json

一个简单的例子:

下面的例子是采用databind 的方式生成 json 串的。

ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(person);

System.out.println(json);

序列化的配置:

//you can indent the json output. just use during testing.
objectMapper.configure(INDENT_OUTPUT, true);
//provide date formatter
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd_HH/mm/ss");
objectMapper.setDateFormat(simpleDateFormat);
//change the name of json properties, use some display strategy
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
//handle null value
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

使用tree api 手动生成或者解析

使用tree api 手动构建一个json 串。

//用来手动创建节点。
JsonNodeFactory jsonNodeFactory = JsonNodeFactory.instance;
//jsonfactory 就是定义输入和输出的位置的
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator jsonGenerator = jsonFactory.createGenerator(System.out);
//首先创建一个root 节点
ObjectNode root = jsonNodeFactory.objectNode();
root.put("username", "zhangsan");
root.put("age", 14);
root.put("friend", "wangwu");
ArrayNode arrayNode = jsonNodeFactory.arrayNode();
arrayNode.add(1);
arrayNode.add(2);
arrayNode.add(3);
arrayNode.add(4);
//然后创建一个 array 节点
root.put("ages",arrayNode);
ObjectMapper objectMapper = new ObjectMapper(jsonFactory);
objectMapper.writeTree(jsonGenerator, root);

使用tree api 解析一个json 串


String json = "{\"username\":\"zhangsan\",\"age\":14,\"friend\":\"wangwu\",\"ages\":[1,2,3,4]}";

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(json);

//解析基本的数据类型。
String username = jsonNode.findValue("username").asText();
int age = jsonNode.findValue("age").asInt();
//解析数组
JsonNode arrayNode = jsonNode.findValue("ages");
Iterator<JsonNode> iterator = arrayNode.iterator();
List<Integer> ages = new ArrayList<Integer>();
while (iterator.hasNext()) {

    int temAge = iterator.next().asInt();
    ages.add(temAge);
}


Person temPerson = new Person(username, age, new Date());

System.out.println(temPerson);
System.out.println(ages);

使用高级api data-bind 来解析数据。

对于开发者来首,面对的最多的是 java pojo。那么data-bind api 就是负责 将一个pojo对象解析成一个json串或者从一个json串生成一个pojo实例。

data-bind 解析和生成的例子。

//解析。
String json = "{\"username\":\"zhangsan\",\"age\":14}";
ObjectMapper objectMapper = new ObjectMapper();
Person person = objectMapper.readValue(json, Person.class);
System.out.println(person);
//生成
Person zhangsanPerson = new Person("zhangsan", 16, new Date());
ObjectMapper objectMapper1 = new ObjectMapper();
String jsonResult = objectMapper1.writeValueAsString(zhangsanPerson);

System.out.println(jsonResult);

如果pojo的某些属性不想生成

  1. 你可以使用类级别的注解(@JsonIgnoreProperties),来屏蔽一些字段。就像:
@JsonIgnoreProperties({"username"})
public class Person {
    ....
}

在这个例子中,字段username就被屏蔽了。从一个json串生成时,username将会是null。从一个Person 实例生成json 时,将不会有username的信息。

结果就像下面一样:

Person{username='null', age=14, birthday=null}
{"age":16,"birthday":1468377397642,"firent":null}
  1. 使用字段级别的注解(@)
public class Person {

    @JsonIgnore
    private String username;
}

结果也像上面一样。

一些注解的含义

  1. @JsonProperty-This annotation is used to mark a method as a getter or setter for a property.
  2. @JsonCreator-This annotation is used the define constructors that are used to create java objects from json string.
  3. @JsonAnyGetter and @JsonAnySetter - This annotations are used to mark methods that set or read fields that are not handled by any other java property.

对于3 。来说是这个样子的,就是如果Jackson 在遇到pojo没有定义的属性时,就调用有3 注解的标签。来存放这些属性。

更详细的注解请看详细的注解说明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值