pom中Jackson详细介绍

Jackson是基于Java平台的一套数据处理工具,被称为”最好的Java Json解析器”。它可以使我们高效、简便的处理json字符串。

我想回忆一下我在pom文件中的架包jackson-core是什么

然后发现就是解析处理json的东西

1、Jackson的版本以及maven依赖重点内容

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.4</version> </dependency>

2、将java对象转换为json字符串

public class Goods {

    //商品id
    private BigInteger goodsId;


    private String data; //商品是否可用标志,true可用,false不可用 private String status; //数据插入或者更新时间 private String ctime; //商品版本号 private Long version; public BigInteger getGoodsId() { return goodsId; } public void setGoodsId(BigInteger goodsId) { this.goodsId = goodsId; } public String getData() { return data; } public void setData(String data) { this.data = data; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getCtime() { return ctime; } public void setCtime(String ctime) { this.ctime = ctime; } public Long getVersion() { return version; } public void setVersion(Long version) { this.version = version; } }
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; public class TestWriteJson1 { private ObjectMapper OBJECT_MAPPER = new ObjectMapper() .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) //类级别的设置,JsonInclude.Include.NON_EMPTY标识只有非NULL的值才会被纳入json string之中,其余的都将被忽略 .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) //禁止使用出现未知属性之时,抛出异常 .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);//转化后的json的key命名格式 @Test public void writeJson() throws JsonProcessingException { Goods goods = new Goods(); goods.setGoodsId("1234"); goods.setData("data"); goods.setCtime("2017-02-24"); String jsonStr = OBJECT_MAPPER.writeValueAsString(goods); System.out.println("JSON:" + jsonStr); } }
扫描二维码关注公众号,回复: 105383 查看本文章

3、String对象转为json格式

public class TestWriteJson2 {

     private ObjectMapper OBJECT_MAPPER = new ObjectMapper()
            .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)//类级别的设置,JsonInclude.Include.NON_EMPTY标识只有非NULL的值才会被纳入json string之中,其余的都将被忽略 .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)//禁止使用出现未知属性之时,抛出异常 .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);//转化后的json的key命名格式 @Test public void writeJson() throws JsonProcessingException { //1、str对象的属性具有很强的统一性,属性固定 String str = ""; Goods goods = null; try { goods = new ObjectMapper().readValue(str, new TypeReference<Goods>() { }); } catch (IOException e) { } if(goods != null){ //取出相应属性 } //2、str对象的属性没有很强的统一性,有可能属性缺失 String str = ""; // Jackson提供一个树节点被称为"JsonNode",ObjectMapper提供方法来读json作为树的JsonNode根节点 JsonNode jsonNode = objectMapper.readTree(valueParser); List<JsonNode> dataList = jsonNode.findValues("data"); if(dataList != null && dataList.size() > 0){ //取出相应属性 // 得到所有node节点的子节点名称 Iterator<String> fieldNames = dataList.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); System.out.print(fieldName+" "); } //得到某个属性值 JsonNode goodId = dataList.get("goodId"); } } }

猜你喜欢

转载自www.cnblogs.com/jingluu/p/8985407.html