• 使用 jackson 解析 json 演示样例



    首先须要下载3个包,下载地址在Github FasterXML,这三个核心模块各自是:
    Streaming ("jackson-core") defines low-level streaming API, and includes JSON-specific implementations
    Annotations ("jackson-annotations") contains standard Jackson annotations
    Databind ("jackson-databind") implements data-binding (and object serialization) support on streaming package;
                                             it depends both on streaming and annotations packages。

    json文件:
    <span style="font-family:SimHei;font-size:18px;">{
         "name":" vonzhou",
         "age":123,
         "isMan":true
    }</span>


    POJO类:
    <span style="font-family:SimHei;font-size:18px;">package jackson.test;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    public class ExamplePOJO {
          @JsonProperty( "name")
          private String name;
         
          @JsonProperty( "age")
          private Integer age;
         
          @JsonProperty( "isMan")
          private Boolean isMan;
         
          private Map<String,Object> additionalProperties = new
                    HashMap<String, Object>();
         
          @JsonProperty( "name")
          public String getName() {
                return name;
         }
          @JsonProperty( "name")
          public void setName(String name) {
                this. name = name;
         }
          @JsonProperty( "age")
          public Integer getAge() {
                return age;
         }
          @JsonProperty( "age")
          public void setAge(Integer age) {
                this. age = age;
         }
          @JsonProperty( "isMan")
          public Boolean getIsMan() {
                return isMan;
         }
          @JsonProperty( "isMan")
          public void setIsMan(Boolean isMan) {
                this. isMan = isMan;
         }
    
          public Map<String, Object> getAdditionalProperties() {
                return additionalProperties;
         }
    
          public void setAdditionalProperties(String name, Object obj) {
                this. additionalProperties.put(name, obj);
         }
         
          @Override
          public String toString() {
                return "ExamplePOJO{
    " +
                          "name :'" + name + "',
    " +
                          "age : " + age + ",
    " +
                          "isMan :" + isMan + ",
    " +
                          "additionalProperties : " + additionalProperties +
                          "
    }";
         }
         
    
    }
    
    </span>


    解析json文件的类:
    <span style="font-family:SimHei;font-size:18px;">package jackson.test;
    
    import java.io.File;
    import java.io.IOException;
    
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class Driver {
         public static void main(String[] args) {
              ObjectMapper mapper = new ObjectMapper();
              try {
                   ExamplePOJO bean = mapper.readValue(new File("test.json"), 
                                                                     ExamplePOJO.class);
                   System.out.println("name : " + bean.getName());
                   System.out.println("age : " + bean.getAge() );
                   System.out.println("isMan : " + bean.getIsMan() );
                   System.out.println("===================");
                   System.out.println(bean.toString());
              } catch (JsonParseException e) {
                   e.printStackTrace();
              }catch (JsonMappingException e){
                   e.printStackTrace();
              }catch (IOException e){
                   e.printStackTrace();
              }             
         }
    }</span>


  • 相关阅读:
    【原创】QTP中手动添加对象
    【转载】【缺陷预防技术】流程技术预防
    【资料】HP Loadrunner 11下载地址
    使用命令行操作VSS
    sql server 按时间段查询记录的注意事项
    Asp.net应用程序文件名重名引起的bug
    使用SQL语句查询表中重复记录并删除
    backgroundpositionx的兼容性问题
    关于Asp.net Development Server
    如何查看正在使用某个端口的应该程序
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5425112.html
Copyright © 2020-2023  润新知