JSON:(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript(欧洲计算机协会制定的js规范)的一个子集,采用彻底独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提高网络传输效率。目前,Json处于数据交换语言的王者地位。javascript
Json的数据自己是数组,中括号包裹。数组的元素之间用“,”
分开,数组元素的数据类型没有限制。java
//定义数组格式json var jsonArray = ["a1","a2",...]; //打印json数组的a1元素 console.log(jsonArray[0]);
Json的数据自己是对象,大括号去包裹。对象采用键值对形式存储,键固定为字符串类型,值是任意类型的数据,键和值使用“:”
分开。spring
//定义对象格式json var jsonObject = {"k1":"v1","k2":"v2"} //打印json对象键k1的值 console.log(jsonObject.k1);
数组中的元素是对象编程
var jsonArray = [ {"k1":"v1"},{"k2":"v2"} ]; // 定义数组格式json,数组元素是对象 console.log(jsonArray[0].k1); //访问数组0索引的元素,该元素的键k1对应的值
对象中的值是数组json
var jsonObject = { "k1":["a1","a2"], "k2":["b1","b2"] }; // 定义对象格式json,键是字符串类型,值是数组 console.log(jsonObject.k1[0]); //访问对象的键是k1,对于的值为数组,数组的0索引元素
互相嵌套api
var json = { "k1":[ "a1",{"key1":"value1"},{"key2":"value2"} ], "k2":[ {"key1":"value1"} ] }; //定义对象格式json,键是字符串,值是数组,数组的元素是对象 console.log(json.k1[1].key1); //访问json对象的键k1,对应的是数组,访问数组的1索引,数组的1索引上的元素是对象,访问key1键对应的值
FastJson 是阿里巴巴的开源JSON解析库,它能够解析 JSON 格式的字符串,支持将 Java Bean 序列化为 JSON 字符串,也能够从 JSON 字符串反序列化到 JavaBean。数组
Fastjson 的优势spring-mvc
序列化:是指将Java对象转成json格式字符串的过程。Java Bean对象、List集合对象、Map集合,为应用最普遍的。服务器
JSON.toJSONString网络
Java
对象public void objectToJSon(){ Student student = new Student("张三",22,getDate()); String str = JSON.toJSONString(student); System.out.println(str); }
List
集合public void listToJson(){ Student student1 = new Student("张三",22,getDate()); Student student2 = new Student("李四",20,getDate()); List<Student> list = new ArrayList<Student>(); list.add(student1); list.add(student2); String jsonString = JSON.toJSONString(list); System.out.println(jsonString); }
Map
集合public void mapToJson(){ Student student1 = new Student("张三",22,getDate()); Student student2 = new Student("李四",20,getDate()); Map<String,Student> map = new HashMap<String, Student>(); map.put("s1",student1); map.put("s2",student2); String jsonString = JSON.toJSONString(map); System.out.println(jsonString); }
JSON.parseObject
Java
对象public void jsonToObject(){ String jsonString = "{\"age\":20,\"name\":\"张三\",\"birthday\":1617673647826}"; Student student = JSON.parseObject(jsonString, Student.class); System.out.println(student); }
JSON.parseArray
List
集合public void jsonToList(){ String jsonString = "[{\"age\":22,\"birthday\":1617673647826,\"name\":\"张三\"},{\"age\":20,\"birthday\":1617673647826,\"name\":\"李四\"}]"; List<Student> list = JSON.parseArray(jsonString,Student.class); for (int i = 0; i < list.size(); i++) { Student student = list.get(i); System.out.println(student); } }
JSON.parseObject
Map
集合public void jsonToMap(){ String jsonString = "{\"s1\":{\"age\":22,\"birthday\":1617673921907,\"name\":\"张三\"},\"s2\":{\"age\":20,\"birthday\":1617673921907,\"name\":\"李四\"} }"; Map<String,Student> parse = JSON.parseObject(jsonString,new TypeReference<Map<String,Student>>(){}); for(String s : parse.keySet()){ System.out.println(s + ":::"+parse.get(s)); } }
该枚举支持序列化的一些特性数据定义。
枚举常量 WriteMapNullValue
序列化为null
的字段
public void testSerializerFeature(){ Student student = new Student(); student.setAge(20); //student.setName("chen"); student.setBirthday(getDate()); String jsonString = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue); System.out.println(jsonString); }
枚举常量 WriteNullStringAsEmpty
字段为null
,序列化为""
public void testSerializerFeature(){ Student student = new Student(); student.setAge(20); //student.setName("chen"); student.setBirthday(getDate()); String jsonString = JSON.toJSONString(student, SerializerFeature.WriteNullStringAsEmpty); System.out.println(jsonString); }
枚举常量 WriteNullNumberAsZero
字段为null
,序列化为0
public void testSerializerFeature(){ Student student = new Student(); //student.setAge(20); student.setName("chen"); student.setBirthday(getDate()); String jsonString = JSON.toJSONString(student, SerializerFeature.WriteNullNumberAsZero); System.out.println(jsonString); }
枚举常量 WriteNullBooleanAsFalse
字段值为null
输出false
枚举常量 WriteDateUseDateFormat
格式化日期格式
枚举常量 PrettyFormat
格式化输出
该注解做用于方法上,字段上和参数上。可在序列化和反序列化时进行特性功能定制
name
序列化后的名字; ordinal
序列化后的顺序;format
序列化后的格式;serialize
是否序列化该字段;deserialize
是否反序列化该字段;serialzeFeatures
序列化时的特性定义。该注解做用于类上,对该类的字段进行序列化和反序列化时的特性功能定制
includes
要被序列化的字段; orders
序列化后的顺序;serialzeFeatures
序列化时的特性定义。SpringMVC框架中,默认使用的json序列化工具是jackson,咱们须要在SpringMVM的配置文件中,配置消息转换器,由jackson切换到FastJson。
<!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
<mvc:annotation-driven> <mvc:message-converters register-defaults="false"> <!-- FastJson的消息转换器--> <bean id = "fastJson" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <!-- FastJsonHttpMessageConverter类属性赋值--> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
@ResponseBody @RequestMapping("/allBook") public String list() { List<Books> list = bookService.queryAllBook(); String jsonString = JSON.toJSONString(list); //System.out.println(jsonString); return jsonString; }