【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据

【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据

文/传智播客郑州校区就业服务部

首先我们创建一个maven工程,如下图:
【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
第二步:配置pom.xml
<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.4.1.RELEASE</version>
  </parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.7 ,默认是1.6 -->
    <java.version>1.7</java.version>
  </properties>
  
  <dependencies>
          <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
   
    <!-- 添加fastjson 依赖包. -->
        <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.15</version>
        </dependency>
        
        <!-- spring boot devtools 依赖包. -->
        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-devtools</artifactId>
           <optional>true</optional>
          <scope>true</scope>
        </dependency>
   
  </dependencies>
第三步:创建如下图所示的包结构和类
【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
第四步:编写上面三个类
User:
【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
提供get、set方法。
FastJsonController:
【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
App:
【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
上述步骤完成后,执行App类的main方法,等待项目启动完毕。然后在浏览器中访问
http://localhost:8080/getJson,返回结果如下图:
【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
发现中文乱码,这时修改controller类,如下图:

【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
然后再次访问,结果如下图:
【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
解决乱码问题。

【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
传智播客·黑马程序员郑州校区地址
河南省郑州市 高新区长椿路11号大学科技园(西区)东门8号楼三层
联系电话 0371-56061160/61/62
来校路线  地铁一号线梧桐街站A口出