Detailed introduction of Jackson in pom

Jackson is a set of data processing tools based on the Java platform, known as "the best Java Json parser". It allows us to process json strings efficiently and easily.

 

 

I want to recall what my shelf package jackson-core is in my pom file

Then I found that it was something that parsed and processed json

1. Jackson version and maven dependency key content

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

 

2. Convert java object to json string

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); } }

 

3. Convert String object to json format

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"); } } }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325212420&siteId=291194637