news 2026/4/25 3:23:31

json与fastjson

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
json与fastjson

json是一种轻量级数据交换格式,易于人阅读和编写,同时也易于机器解析和生成

json数据格式

json数组

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript"> /** * 定义json的数组格式 *中括号包裹,数组的元素的数据没有限制 * 元素之间逗号隔开 */ var jsonArray = ["k1","k2",100,9.9,true] // 遍历数组,取出数组中的元素 for (var i = 0 ; i < jsonArray.length ; i++){ console.log(jsonArray[i]); } </script> </head> <body> </body> </html>

json对象

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript"> /** * 定义json的对象格式 * 大括号包裹,定义赋值对,键必须是字符串类型,值的数据类型不限制 * 键值对之间是冒号分开的 * 每个键值对之间逗号分开 */ var jsonObject = {"k1":"v1","k2":"v2","k3":100,"k4":9.9,"k5":true}; // 取出键值对,键找值的方式 console.log(jsonObject.k1); console.log(jsonObject.k2); console.log(jsonObject.k3); console.log(jsonObject.k4); console.log(jsonObject.k5); </script> </head> <body> </body> </html>

json嵌套

数组套对象

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>json数据的嵌套</title> </head> <body> <script type="text/javascript"> /** * json数组的元素是对象 * 数组定义2个元素,每个元素是对象 * 对象是键值对形式 */ var jsonArray = [ {"name":"张三","age":20}, {"name":"里斯","age":22} ]; // 取出重要的数据,李四22 console.log( jsonArray[1].name +" == "+ jsonArray[1].age); // 遍历数组,取出数组中的元素 for(var i=0 ; i< jsonArray.length; i++){ console.log(jsonArray[i].name + " == "+ jsonArray[i].age); } </script> </body> </html>

对象套数组

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>json数据嵌套</title> </head> <body> <script type="text/javascript"> /** * json数据是对象,对象只是数组 */ var jsonObject = { "k1": ["北京,“天津","上海"], "k2": ["中国","美国","英国"] }; // 取出上海 console.log( jsonObject.k1[2]) // 分别取出看和可的数组,遍历 for (var i =0; i< jsonObject.k1.length; i++){ console.log(jsonObject.k1[i]); } console.log("===========") for (var i =0; i< jsonObject.k2.length; i++){ console.log(jsonObject.k2[i]); } </script> </body> </html>

你中有我,我中有你

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数据嵌套</title> </head> <body> <script type="text/javascript"> /** * json的数据嵌套,你中有我,我中有你 * json的数据本质上是对象 * 对象的键是字符串 * 数组的元素是对象 */ var json= { // 键是K1值是数组,数组的元素是对象 "k1":[ {"name":"张三","age":20}, {"name":"里斯","age":22} ], "k2":[ {"name":"王五","age":24}, {"name":"赵六","age":26} ] }; // 取出数据李四26 console.log(json.k1[1].name+"==="+json.k1[1].age) // 遍历k2键对应的数组 for(var i=0 ; i< json.k2.length; i++) { console.log(json.k2[i].name + " == " + json.k2[i].age); } </script> </body> </html>

fastjson

他可以解析json格式的字符串,支持将Java Bean序列化为json字符串,也可以从json字符串反序列化到javaBean。

package src.FastJson; import lombok.Data; import java.util.Date; @Data public class Student { private Integer id; private String name; private Integer age; private String email; private Date birthday; }

序列化

对象转json

@Test // java中的对象,Student对象,序列化为json格式字符串 public void testObjectToJson(){ Student student = new Student(); student.setId(1); student.setName("张三"); student.setAge(20); student.setEmail("zs@www.com"); student.setBirthday(getDate()); // student对象,转到json格式字符串 String jsonString = JSON.toJSONString(student); System.out.println(jsonString); // {"age":20,"birthday":0,"email":"zs@www.com","id":1,"name":"张三"} }

List转json

@Test // java中的集合List,序列化为json格式字符串 public void testListToJson() { //集合List,存储Student对象 List<Student> list = new ArrayList<Student>(); Student student1 = new Student(); student1.setId(1); student1.setName("张三"); student1.setAge(20); student1.setEmail("zs@www.com"); student1.setBirthday(getDate()); Student student2 = new Student(); student2.setId(2); student2.setName("张"); student2.setAge(20); student2.setEmail("z@www.com"); student2.setBirthday(getDate()); // Student对象存储到List集合中 list.add(student1); list.add(student2); // List集合序列化为json格式字符串 String jsonString = JSON.toJSONString(list); System.out.println(jsonString); //转后的元素是数组,数组的元素是对象 // [{"age":20,"birthday":0,"email":"zs@www.com","id":1,"name":"张三"}, // {"age":20,"birthday":0,"email":"z@www.com","id":2,"name":"张"}] }

Map转json

@Test // java中的集合Map,序列化为json格式字符串 public void testMapToJson() { // 创建Map集合,键为字符串类型,值是Student对象 Map<String,Student> map = new HashMap<String,Student>(); Student student1 = new Student(); student1.setId(1); student1.setName("张三"); student1.setAge(20); student1.setEmail("zs@www.com"); student1.setBirthday(getDate()); Student student2 = new Student(); student2.setId(2); student2.setName("张"); student2.setAge(20); student2.setEmail("z@www.com"); student2.setBirthday(getDate()); // Map集合中存储student对象 map.put("student1", student1); map.put("student2", student2); String jsonString = JSON.toJSONString(map); System.out.println(jsonString); // json格式字符串是对象,对象有两个键,键对应的值是Student对象 // [{"age":20,"birthday":0,"email":"zs@www.com","id":1,"name":"张三"}, // {"age":20,"birthday":0,"email":"z@www.com","id":2,"name":"张"}] }

反序列化

json转对象

@Test // json格式字符串,反序列化回到java对象 public void testJsonToObject() { String jsonString = "{\"age\":20,\"birthday\":0,\"email\":\"zs@www.com\",\"id\":1,\"name\":\"张三\"}"; // JSON类的静态方法parseObject // 传递要反序列化的json字符串 Student student = JSON.parseObject(jsonString,Student.class); System.out.println(student); // Student(id=1, name=张三, age=20, email=zs@www.com, birthday=Thu Jan 01 08:00:00 CST 1970) }

json转list

@Test public void testJsonToList() { String jsonString = "[{\"age\":20,\"birthday\":0,\"email\":\"zs@www.com\",\"id\":1,\"name\":\"张三\"},{\"age\":20,\"birthday\":0,\"email\":\"z@www.com\",\"id\":2,\"name\":\"张\"}]"; // JSON类的静态方法,parseArray // 传递json格式字符串,传递转换后的集合的泛型class对象 List<Student> list = JSON.parseArray(jsonString,Student.class); for (Student student : list) { System.out.println(student); } // Student(id=1, name=张三, age=20, email=zs@www.com, birthday=Thu Jan 01 08:00:00 CST 1970) // Student(id=2, name=张, age=20, email=z@www.com, birthday=Thu Jan 01 08:00:00 CST 1970) }

json转map

@Test // JSON格式字符串反序列化到Map public void testJsonToMap() { String jsonString = "{\"1\":{\"age\":20,\"birthday\":0,\"email\":\"zs@www.com\",\"id\":1,\"name\":\"张三\"},\"2\":{\"age\":20,\"birthday\":0,\"email\":\"z@www.com\",\"id\":2,\"name\":\"张\"}}"; // json类的静态方法,paresObject // 直接进行反序列化2,Map没有泛型,泛型没有是不安全的集合 // 转后的集合,必须有泛型 // 调用parseObject,传递参数,T类型,在T类的泛型,传递 Map map = JSON.parseObject(jsonString,new TypeReference<Map<String,Student>>(){}); for (Object key: map.keySet()){ System.out.println(key+":"+map.get(key)); } // 1:Student(id=1, name=张三, age=20, email=zs@www.com, birthday=Thu Jan 01 08:00:00 CST 1970) // 2:Student(id=2, name=张, age=20, email=z@www.com, birthday=Thu Jan 01 08:00:00 CST 1970) }

fastjson枚举

import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import org.junit.Test; import java.util.Date; /** * SerializerFeature枚举:进行序列化时,可以自己定义特殊需求 * json静态方法 toJSONString() * 方法的参数:第一个是要序列化的对象 * 方法的参数:第二个参数SerializerFeature枚举类型的可变参数 * SerializerFeature枚举的常量,做序列化的个性需求 */ public class TestFastJson2 { @Test // WriteNullNumberAsZero 枚举的常量,序列化为null的字段,序列化为零 public void testWriteNullNumberAsZero(){ Student student = new Student(); student.setId(1); student.setName("张三"); // student.setAge(20); student.setEmail("zs@www.com"); student.setBirthday(getDate()); // 方法的参数上添加枚举类型 // 只适用于数字 String jsonString = JSON.toJSONString(student,SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullNumberAsZero); System.out.println(jsonString); // {"age":0,"birthday":0,"email":"zs@www.com","id":1,"name":"张三"} } @Test // WriteNullStringAsEmpty枚举的常量,序列化为null的字段,值序列化为“” public void testWriteNullStringAsEmpty(){ Student student = new Student(); student.setId(1); student.setName("张三"); student.setAge(20); // student.setEmail("zs@www.com"); student.setBirthday(getDate()); // 方法的参数上添加枚举类型 String jsonString = JSON.toJSONString(student,SerializerFeature.WriteNullStringAsEmpty); System.out.println(jsonString); // {"age":20,"birthday":0,"email":"","id":1,"name":"张三"} } @Test //writeMapNullValue 枚举中的常量,序列化null值的字段 public void testWriteMapNullValue(){ Student student = new Student(); student.setId(1); student.setName("张三"); student.setAge(20); // student.setEmail("zs@www.com"); student.setBirthday(getDate()); // 方法的参数上添加枚举类型 String jsonString = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue); System.out.println(jsonString); // {"age":20,"birthday":0,"email":null,"id":1,"name":"张三"} } public Date getDate() { Date date = new Date(0); return date; } }

json注解

jsonfield注解

用于序列化时进行特性定制

@JSONfield注解属性name,指定序列化后的名字

ordinal,指定序列化后的字段顺序

format,指定序列化后的格式

serialize,指定是否序列化该字段

JSonType注解

注解作用于类上

includes要被序列化的字段

order要被序列化的字段的顺序

serialzeFeatures序列化时的特性定义

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/25 3:20:58

Meta计划5月裁员约10%,约8000人受影响,此前AI领域投资巨大

Meta新一轮裁员&#xff1a;约8000人将告别据彭博社公布的Meta首席人力官珍妮尔盖尔&#xff08;Janelle Gale&#xff09;的备忘录显示&#xff0c;Meta计划在5月裁员约10%&#xff0c;这意味着约8000人将被裁。同时&#xff0c;盖尔还表示&#xff0c;Meta还将关闭约6000个招…

作者头像 李华
网站建设 2026/4/25 3:19:30

颜色科学避坑指南:CIE Lab转sRGB时,你的D65白点参数设置对了吗?

颜色科学避坑指南&#xff1a;CIE Lab转sRGB时&#xff0c;你的D65白点参数设置对了吗&#xff1f; 在数字图像处理领域&#xff0c;颜色空间的转换看似简单&#xff0c;实则暗藏玄机。许多开发者和设计师都曾遇到过这样的困惑&#xff1a;明明按照标准公式实现了从CIE Lab到sR…

作者头像 李华
网站建设 2026/4/25 3:16:10

JavaBean规则与JSP使用全攻略

JavaBean 编写规则与 JSP 使用指南编写 JavaBean 的规则JavaBean 是一种符合特定规范的 Java 类&#xff0c;主要用于封装数据和业务逻辑。公有无参构造方法 每个 JavaBean 必须包含一个公共的无参构造方法&#xff0c;便于容器或框架通过反射机制实例化对象。类中的属性要有相…

作者头像 李华
网站建设 2026/4/25 3:14:03

深入SOEM源码:SDO读写函数背后的EtherCAT邮箱通信机制与性能调优

深入SOEM源码&#xff1a;SDO读写函数背后的EtherCAT邮箱通信机制与性能调优 在工业自动化领域&#xff0c;EtherCAT以其卓越的实时性能和高效的通信机制成为众多高精度运动控制系统的首选。作为EtherCAT主站开源实现&#xff0c;SOEM&#xff08;Simple Open EtherCAT Master&…

作者头像 李华
网站建设 2026/4/25 3:11:29

洛谷-算法2-1-前缀和、差分与离散化2

P1955 [NOI2015] 程序自动分析 题目描述 在实现程序自动分析的过程中&#xff0c;常常需要判定一些约束条件是否能被同时满足。 考虑一个约束满足问题的简化版本&#xff1a;假设 x1​,x2​,x3​,⋯ 代表程序中出现的变量&#xff0c;给定 n 个形如 xi​xj​ 或 xi​xj​ …

作者头像 李华