#阿里巴巴fastJson #
Fastjson是一个Java语言编写的JSON处理器。
1、遵循http://json.org标准,为其官方网站收录的参考实现之一。
2、功能强大,支持JDK的各种类型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。
3、无依赖,不需要例外额外的jar,能够直接跑在JDK上。
4、开源,使用Apache License 2.0协议开源。http://code.alibabatech.com/wiki/display/FastJSON/Home
1 准备工作:
maven(jar) 依赖:
https://mvnrepository.com/artifact/com.alibaba/fastjson
Api文档:
GitHub: https://github.com/alibaba/fastjson/wiki/JSON_API_cn
w3c: https://www.w3cschool.cn/fastjson/fastjson-jsonfield.html
序列化API
package com.alibaba.fastjson;
public abstract class JSON {
// 将Java对象序列化为JSON字符串,支持各种各种Java基本类型和JavaBean
public static String toJSONString(Object object, SerializerFeature... features);
// 将Java对象序列化为JSON字符串,返回JSON字符串的utf-8 bytes
public static byte[] toJSONBytes(Object object, SerializerFeature... features);
// 将Java对象序列化为JSON字符串,写入到Writer中
public static void writeJSONString(Writer writer,
Object object,
SerializerFeature... features);
// 将Java对象序列化为JSON字符串,按UTF-8编码写入到OutputStream中
public static final int writeJSONString(OutputStream os, //
Object object, //
SerializerFeature... features);
}
JSON字符串反序列化API
package com.alibaba.fastjson;
public abstract class JSON {
// 将JSON字符串反序列化为JavaBean
public static T parseObject(String jsonStr,
Class clazz,
Feature... features);
// 将JSON字符串反序列化为JavaBean
public static T parseObject(byte[] jsonBytes, // UTF-8格式的JSON字符串
Class clazz,
Feature... features);
// 将JSON字符串反序列化为泛型类型的JavaBean
public static T parseObject(String text,
TypeReference type,
Feature... features);
// 将JSON字符串反序列为JSONObject
public static JSONObject parseObject(String text);
}
2 新建pojo
public class User {
private String name;
private Integer id;
private String grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
3 创建对象
User user = new User();
user.setGrade("1班");
user.setId(1);
user.setName("zs");
User user2 = new User();
user2.setGrade("2班");
user2.setId(2);
user2.setName("ls");
//新建数组
List list = new ArrayList();
list.add(user);
list.add(user2);
4 序列化与反序列化:
1、 pojo 转 Json 字符串
String s1 = JSON.toJSONString(user);
System.out.println(s1);
输出
{"grade":"1班","id":1,"name":"zs"}
2、 json字符串 转 json 对象
JSONObject jsonObject = JSONObject.parseObject(s1);
Object id = jsonObject.get("id"); //拿到id
System.out.println(id);//1
System.out.println("json对象转json字符串 "+jsonObject.toJSONString());
输出
json对象转json字符串 {"grade":"1班","name":"zs","id":1}
1
3、 json字符串 转 pojo 对象
User user1 = JSON.parseObject(s1, User.class);
System.out.println(user1.getName());
输出
zs
4、 json字符串 转 list 对象
List users = JSONArray.parseArray(s1, User.class);
users.forEach(obj-> System.out.println(obj.toString()));
输出
com.alibaba.fastJson.User@762efe5d
com.alibaba.fastJson.User@5d22bbb7
5、 list转json字符串
Object toJSON = JSON.toJSON(list);
System.out.println("list-->jsonStr"+toJSON);
输出
list转json字符串[{"grade":"1班","id":1,"name":"zs"},{"grade":"2班","id":2,"name":"ls"}]
6、 json字符串 转 Map
String str = "{\"1\":\"zs\",\"2\":\"ls\",\"4\":\" ww\",\"5\":\"ml\"}";
//第一种
Map maps = (Map) JSON.parse(str);
System.out.println("第1种");
for (Object obj : maps.keySet()) {
System.out.println("key:" + obj + "value:" + maps.get(obj));
}
//第二种
Map mapTypes = JSON.parseObject(str);
System.out.println("第2种");
for (Object obj : mapTypes.keySet()) {
System.out.println("key:" + obj + "value:" + mapTypes.get(obj));
}
//第三种
Map mapType = JSON.parseObject(str, Map.class);
System.out.println("第3种");
for (Object obj : mapType.keySet()) {
System.out.println("key:" + obj + "value:" + mapTypes.get(obj));
}
//第四种
/**
* JSONObject是Map接口的一个实现类
*/
Map json = (Map) JSONObject.parse(str);
//第五种
Map jsonMap = (Map) JSONObject.parseObject(str);
输出
第1种
key:1value:zs
key:2value:ls
key:4value: ww
key:5value:ml
第2种
key:1value:zs
key:2value:ls
key:4value: ww
key:5value:ml
第3种
key:1value:zs
key:2value:ls
key:4value: ww
key:5value:ml