做网站主机电脑,千锋教育培训多少钱,自我介绍的网页设计作业,wordpress灯箱zoom目录
FastJson
新建一个SpringBoot项目
pom.xml
一、JavaBean与JSON数据相互转换
LoginController FastJsonApplication启动类
编辑二、FastJson的JSONField注解
Log实体类 TestLog测试类
三、FastJson对JSON数据的增、删、改、查
TestCrud FastJson 1、JSON使用手册…目录
FastJson
新建一个SpringBoot项目
pom.xml
一、JavaBean与JSON数据相互转换
LoginController FastJsonApplication启动类
编辑二、FastJson的JSONField注解
Log实体类 TestLog测试类
三、FastJson对JSON数据的增、删、改、查
TestCrud FastJson 1、JSON使用手册JSON 教程 | 菜鸟教程 (runoob.com)2、FastJson官方文档Quick Start CN · alibaba/fastjson Wiki (github.com)3、 JSONJavaScript Object Notation JavaScript 对象标记法是一种轻量级的数据交换格式。对于一个前后端分离的SpringBoot项目而言前端需要的是以“键值”结构保存的JSON数据后端需要的是JavaBean。所以出现了两种JSON解析库把它们转来转去以便前后端进行数据交流 1、Spring Boot内置的Jackson适合场景复杂、业务量大的项目2、阿里巴巴开发的FastJson适合数据量小、并发量小的项目FastJson是JSON解析库用于转换JavaBean和JSON数据 序列化将Java对象转换为JSON字符串 反序列化将JSON字符串转换为Java对象 String text JSON.toJSONString(obj); //序列化
VO vo JSON.parseObject({...}, VO.class); //反序列化
//VO与JSON数据对应的实体类 JSONField注解 当你需要更精确地控制Java对象的字段在序列化和反序列化过程中的行为时可以使用JSONField注解JSONField注解可以用于声明类、属性或方法该注解可以让人重新定制序列化规则 增删改查 FastJSON将JSON数据分成“对象”和“数组”两种形式把对象节点封装成JSONObject类把数组节点封装成JSONArray类然后利用这两个类对JSON数据进行增、删、改查操作 新建一个SpringBoot项目 项目结构 pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.12.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.study/groupIdartifactIdfastJson/artifactIdversion0.0.1-SNAPSHOT/versionnamefastJson/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--添加FastJSON依赖--dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.28/version/dependency!--使用Test注解--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project一、JavaBean与JSON数据相互转换
LoginController 接收前端发来的JSON数据返回JSON登录结果 package com.study.fastJson.controller;import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;/*** 接收前端发来的JSON数据,返回JSON登录结果*/
RestController
public class LoginController {RequestMapping(/login)public String login(RequestBody String json){//将请求体中的字符串以JSON格式读取并转换为Map键值对象Map loginDateJSON.parseObject(json,Map.class);//读取JSON中的账号String usernameloginDate.get(username).toString();//读取JSON中的密码String passwordloginDate.get(password).toString();HashMapString, String result new HashMap();//返回的响应码String code;//返回的响应信息String message;if(mr.equals(username) 123456.equals(password)){code200;message登录成功;}else{code500;message账号或密码错误;}//将响应码和响应信息保存到result响应结果中result.put(code,code);result.put(message,message);//将键值对象转换为以键:值结构保存的JSON数据并返回return JSON.toJSONString(result);}
}FastJsonApplication启动类
package com.study.fastJson;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class FastJsonApplication {public static void main(String[] args) {SpringApplication.run(FastJsonApplication.class, args);}}启动启动类使用postman进行测试 二、FastJson的JSONField注解
Log实体类 JSONField注解的几个重要属性 nameserializeformatordinal package com.study.fastJson.entity;import com.alibaba.fastjson.annotation.JSONField;import java.util.Date;/*** JSONField注解的各种常见用法*/
public class Log {//ordinal用于定义不同属性被转换后的JSON数据中的排列顺序,值越大越靠后JSONField(ordinal 0,namecode)//为该属性定义别名codeprivate String id;JSONField(ordinal 1,serialize false)//该属性不会被序列化,即不显示public String message;JSONField(ordinal 2,format yyyy-MM-dd HH:mm:ss)//定义该属性序列化时的日期格式public Date create;public Log() {}public Log(String message, Date create, String id) {this.message message;this.create create;this.id id;}//Getter(),Setter()方法省略
}TestLog测试类
package com.study.fastJson.entity;import com.alibaba.fastjson.JSON;
import org.junit.Test;import java.util.Date;public class TestLog {/*** JSONField(namecode)* 定义属性的别名,以别名使用该属性*/Testpublic void testName(){Log log new Log();log.setId(404);System.out.println(JSON.toJSONString(log));}/*** JSONField(format yyyy-MM-dd HH:mm:ss)* 当Log对象被转换为JSON数据时,会自动按照JSONField注解定义的日期格式进行转换*/Testpublic void testDateFormat(){Log log new Log();log.createnew Date();System.out.println(JSON.toJSONString(log));}/*** JSONField(serialize false)* id属性不会被序列化*/Testpublic void testSerialize(){Log log new Log();log.setId(404);log.message找不到资源;System.out.println(JSON.toJSONString(log));}/*** JSONField(ordinal 0)* ordinal用于定义不同属性被转换后的JSON数据中的排列顺序,值越大越靠后*/Testpublic void testOrder(){Log log new Log(找不到资源,new Date(),404);System.out.println(JSON.toJSONString(log));}
}三、FastJson对JSON数据的增、删、改、查
TestCrud
package com.study.fastJson;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;/*** 使用JSONObject类和JSONArray类对JSON数据进行增删改查操作*/
public class TestCrud {/*** 查询数据* 使用get()方法查询*/Testpublic void getJson(){String json{\name\:\张三\,\age\:25,\qq\:[\123456789\,\987654321\],\scores\:{\chinese\:90,\math\:85}};//获取以键:值结构保存的JSON数据的对象节点JSONObject root JSON.parseObject(json);//查询指定字段对应的值String nameroot.getString(name);int ageroot.getIntValue(age);System.out.println(姓名:name,年龄:age);//获取JSON数组中的值JSONArray arrroot.getJSONArray(qq);String firstQQarr.getString(0);System.out.println(firstQQ);//获取JSON子节点中的数据JSONObject scoresroot.getJSONObject(scores);int mathscores.getIntValue(math);System.out.println(数学成绩为:math);}/*** 增加数据* 对象节点使用put()节点增加,数组节点使用add()方法增加*/Testpublic void addJson(){String json{\name\:\张三\,\age\:25,\qq\:[\123456789\,\987654321\],\scores\:{\chinese\:90,\math\:85}};//获取以键:值结构保存的JSON数据的对象节点JSONObject root JSON.parseObject(json);root.put(sex,男);root.getJSONArray(qq).add(999999);root.getJSONObject(scores).put(english,92);System.out.println(root.toJSONString());}/*** 修改数据* 对象节点使用put()方法修改,数组节点使用set()方法修改*/Testpublic void updateJson(){String json{\name\:\张三\,\age\:25,\qq\:[\123456789\,\987654321\],\scores\:{\chinese\:90,\math\:85}};//获取以键:值结构保存的JSON数据的对象节点JSONObject root JSON.parseObject(json);root.put(name,李四);//名字改成李四root.getJSONArray(qq).set(1,000000);//将第二个qq号改成000000root.getJSONObject(scores).put(math,70);//数学成绩改成70System.out.println(root.toJSONString());}/*** 删除数据* 对象节点和数组节点都使用remove()方法删除*/Testpublic void removeJson(){String json{\name\:\张三\,\age\:25,\qq\:[\123456789\,\987654321\],\scores\:{\chinese\:90,\math\:85}};//获取以键:值结构保存的JSON数据的对象节点JSONObject root JSON.parseObject(json);root.remove(age);//删除年龄字段root.getJSONArray(qq).remove(0);//删除第一个qq号root.getJSONObject(scores).remove(chinese);//删除语文成绩System.out.println(root.toJSONString());}
}