目录
- 前言
- 一、导入fastjson2依赖
- 二、json对象与json数组的创建
- json对象创建:
- json数组创建:
- 三、json对象取值与json数组遍历取值
- json对象取值:
- json数组遍历取值:
- 四、json对象与字符串的转换
- json对象与字符串的转换
- json字符串的字节数组转json对象
- 五、json数组与字符串的转换
- 六、json字符串转java对象的转换
- json字符串转java对象的转换
- java对象转byte数组转换
- 七、json字符串转Map
- 八、json数组转List
前言
fastjson2 是 FASTJSON 项目的重要升级,目标是为下一个十年提供一个高性能的JSON库, fastjson2 性能相比原先旧的 fastjson 有了很大提升,并且 fastjson2 更安全,完全删除autoType白名单,提升了安全性。
关于fastjson2升级指南
fastjson2 的github地址
一、导入fastjson2依赖
maven如下:
<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.20</version></dependency>
二、json对象与json数组的创建
json对象创建:
JSONObject info = new JSONObject();info.put("name", "张三");info.put("age", "18");info.put("地理", "70");info.put("英语", "60");
json数组创建:
JSONObject info1 = new JSONObject();info1.put("name", "张三");info1.put("age", "18");JSONObject info2 = new JSONObject();info2.put("name", "李四");info2.put("age", "19");//把上面创建的两个json对象加入到json数组里JSONArray array = new JSONArray();array.add(info1);array.add(info2);
JSONArray array = new JSONArray();array.add("1班");array.add("2班");array.add("3班");
三、json对象取值与json数组遍历取值
json对象取值:
JSONArray array = new JSONArray();array.add("1班");array.add("2班");array.add("3班");JSONObject school = new JSONObject();school.put("schoolName", "第一中学");school.put("teacher", "刘梅");JSONObject info = new JSONObject();info.put("name", "张三");info.put("age", "18");info.put("gradle",array);info.put("schoolInfo",school);//从info中取值System.out.println(info.getString("name")); //张三System.out.println(info.getIntValue("age"));//18System.out.println(info.getJSONArray("gradle"));//["1班","2班","3班"]System.out.println(info.getJSONObject("schoolInfo"));//{"schoolName":"第一中学","teacher":"刘梅"}
json数组遍历取值:
JSONObject info1 = new JSONObject();info1.put("name", "张三");info1.put("age", "18");JSONObject info2 = new JSONObject();info2.put("name", "李四");info2.put("age", "19");JSONArray array = new JSONArray();array.add(info1);array.add(info2);//遍历获取json数组中对象的值for (int i = 0; i < array.size(); i++) {JSONObject json = array.getJSONObject(i);System.out.println(json.getString("name"));System.out.println(json.getString("age"));}
四、json对象与字符串的转换
json对象与字符串的转换
JSONObject info = new JSONObject();info.put("name", "张三");info.put("age", "18");info.put("地理", "70");info.put("英语", "60");//JSON对象转字符串String str = JSON.toJSONString(info);//JSON字符串转JSON对象JSONObject json = JSONObject.parseObject(str);
json字符串的字节数组转json对象
String str = "{\"name\":\"张三\",\"age\":\"18\",\"地理\":\"70\",\"英语\":\"60\"}";byte[] bytes = str.getBytes();JSONObject data = JSON.parseObject(bytes);
五、json数组与字符串的转换
String text = "[\"张三\",\"李四\",\"王五\"]";//json字符串转json数组JSONArray data = JSON.parseArray(text);//json数组转json字符串String str = JSONArray.toJSONString(data);
六、json字符串转java对象的转换
Student类如下:
public class Student {public String name;public int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Student(String name, int age) {this.name = name;this.age = age;}
}
json字符串转java对象的转换
Student student = new Student("张三", 18);//Student对象转JSON字符串String studentStr = JSON.toJSONString(student);//JSON字符串转Student对象Student data = JSON.parseObject(studentStr, Student.class);
java对象转byte数组转换
Student student = new Student("张三", 18);//Student对象转字节数组byte[] text = JSON.toJSONBytes(student);//字节数组转Student对象Student data = JSON.parseObject(text, Student.class);
七、json字符串转Map
String str="{\n" +"\"gradle\":\"高一\",\n" +"\"number\":\"2\",\n" +"\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +" {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +"}";//json字符串转MapMap<String, Object> map = JSONObject.parseObject(str, new TypeReference<Map<String, Object>>() {});System.out.println(map.get("gradle").toString());System.out.println(map.get("number").toString());System.out.println(map.get("people").toString());
八、json数组转List
String str="{\n" +"\"gradle\":\"高一\",\n" +"\"number\":\"2\",\n" +"\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +" {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +"}";JSONObject strJson = JSONObject.parseObject(str);//获取people数组JSONArray people = strJson.getJSONArray("people");//json数组转ListList<Map> list = people.toJavaList(Map.class);
如果你使用的是老的fastjson1,可以像下面这样转换:
String str="{\n" +"\"gradle\":\"高一\",\n" +"\"number\":\"2\",\n" +"\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +" {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +"}";JSONObject strJson=JSONObject.parseObject(str);//字符串转json对象String people = String.valueOf(strJson.getJSONArray("people"));List<Map<String,String>> list = (List<Map<String,String>>) JSONArray.parse(people);
参考:
fastjson2