使用 jackson 解析 json 演示样例「建议收藏」

使用 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{\n" +
                      "name :'" + name + "',\n" +
                      "age : " + age + ",\n" +
                      "isMan :" + isMan + ",\n" +
                      "additionalProperties : " + additionalProperties +
                      "\n}";
     }
     

}

</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>




版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115208.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • apache24+php8配置

    apache24+php8配置电脑下载安装地址:Xampp安装完成后找到里面的apache》config》httpd.config:首先注释掉默认的路径DocumentRoot”D:/software/Xampp/htdocs”;然后再找到IfModuledir_module在后面追加:<IfModuledir_module>DirectoryIndexindex.phpindex.htmlindex.htmdefault.phpdefault.htmldefault.htmhome.

    2022年7月12日
    16
  • prometheus 表达式

    prometheus 表达式avg_over_time(my_inprogress_requests{job=”mhc”}[5m]offset3m)返回time=1550664637开始向前偏移3分钟之前的五分钟的平均值curl-XGET\’http://mhc-linux:9090/api/v1/query?query=avg_over_time%28my_inprogress_requests{jo…

    2025年7月27日
    2
  • Chrome 小工具: 启动本地应用 (Native messaging)

    Chrome 小工具: 启动本地应用 (Native messaging)

    2022年1月2日
    46
  • Spring Cloud Eureka集群

    Spring Cloud Eureka集群            SpringCloud基础教程[Eureka集群]NetflixEureka介绍SpirngCloudEureka使用NetflixEureka来实现服务注册与发现。它既包含了服务端组件,也包含了客户端组件,并且服务端与客户端均采用java编写,所以Eureka主要适用于通过java实现的分布式系统,或是JVM兼容语言构建的系统。E…

    2022年5月3日
    53
  • [261]Connection reset by peer的常见原因及解决办法[通俗易懂]

    [261]Connection reset by peer的常见原因及解决办法[通俗易懂]1,如果一端的Socket被关闭(或主动关闭,或因为异常退出而引起的关闭),另一端仍发送数据,发送的第一个数据包引发该异常(Connectresetbypeer)。Socket默认连接60秒,60秒之内没有进行心跳交互,即读写数据,就会自动关闭连接。2,一端退出,但退出时并未关闭该连接,另一端如果在从连接中读数据则抛出该异常(Connectionreset)。简单的…

    2022年6月26日
    26
  • 遭遇mysql数据库表满错误

    遭遇mysql数据库表满错误

    2021年8月20日
    63

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号