JSONObject
前言
fastjson2 是 FASTJSON 项目的重要升级,目标是为下一个十年提供一个高性能的JSON库, fastjson2 性能相比原先旧的 fastjson 有了很大提升,并且 fastjson2 更安全,完全删除autoType白名单,提升了安全性。
一、导入fastjson2依赖
maven如下:
pom(本文所有代码仅使用这一个依赖即可):
<dependencies>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.26</version>
</dependency>
</dependencies>
二、json对象与json数组的创建
json对象创建
public class JsonTest {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","瓜怂");
jsonObject.put("age",25);
jsonObject.put("sex","男");
jsonObject.put("phone","1234567890");
System.out.println(jsonObject);//{"phone":"1234567890","sex":"男","name":"瓜怂","age":25}
}
}
json数组创建
public class JsonTest {
public static void main(String[] args) {
//对象创建
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","瓜怂");
jsonObject.put("age",25);
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("name","瓜皮");
jsonObject1.put("age",25);
//数组创建
JSONArray objects = new JSONArray();
objects.put(jsonObject);
objects.put(jsonObject1);
System.out.println(objects);//[{"name":"瓜怂","age":25},{"name":"瓜皮","age":25}]
}
}
三、json对象取值与json数组遍历取值
json对象取值
//遍历
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("A",jsonObject1);
jsonObject2.put("b",objects);
System.out.println(jsonObject.get("name"));//瓜怂
System.out.println(jsonObject.get("age"));//25
System.out.println(jsonObject2);//{"A":{"name":"瓜皮","age":25},"b":[{"name":"瓜怂","age":25},{"name":"瓜皮","age":25}]}
System.out.println(jsonObject2.getJSONObject("A"));//{"name":"瓜皮","age":25}
System.out.println(jsonObject2.getJSONArray("b"));//[{"name":"瓜怂","age":25},{"name":"瓜皮","age":25}]
json数组遍历取值
//遍历数组
for(int i=0;i<objects.length();i++){
JSONObject json = objects.getJSONObject(i);//
System.out.println(json);
System.out.println(json.get("name"));
System.out.println(json.get("age"));
}
四、json对象与字符串的转换
json对象与字符串的转换
//JSON对象转字符串
String jsonString = JSON.toJSONString(jsonObject);
String jsonString1 = JSON.toJSONString(jsonObject1);
System.out.println(jsonString);
System.out.println(jsonString1);
//JSON字符串转JSON对象
System.out.println(JSON.parse(jsonString));
System.out.println(JSON.parse(jsonString1));
json字符串转json对象
String str ="{\n" +
" \"name\": \"Cainfly\",\n" +
" \"age\": 18,\n" +
" \"地理\": \"70\",\n" +
" \"英语\": \"60\"\n" +
"}";
com.alibaba.fastjson.JSONObject jsonObject3 = JSON.parseObject(str);
System.out.println(jsonObject3);//{"name":"Cainfly","英语":"60","地理":"70","age":18}
//json字符串转json数组
com.alibaba.fastjson.JSONArray objects1 = JSON.parseArray(jsonString1);
//json数组转json字符串
String jsonString2 = com.alibaba.fastjson.JSONArray.toJSONString(objects1);
五、json字符串转java对象的转换
Student类如下:
package com.amarsoft.lease; public class Student { private String name; private int age; private String sex; 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Student(String name, int age,String sex) { this.name = name; this.age = age; this.sex = sex; } }
Student student = new Student("lpf", 25, "男");
System.out.println(student);//com.amarsoft.lease.Student@5d5eef3d
//Student对象转JSON字符串
String s1 = JSON.toJSONString(student);
System.out.println(s1);//{"age":25,"name":"lpf","sex":"男"}
//JSON字符串转Student对象
Student data = JSON.parseObject(s1, Student.class);
System.out.println(data);//com.amarsoft.lease.Student@e320068
六、json字符串与Map转换
json字符串转Map
String str = "{\n" +
" \"gradle\": \"高一\",\n" +
" \"number\": \"2\",\n" +
" \"people\": [\n" +
" {\n" +
" \"sex\": \"男\",\n" +
" \"name\": \"张三\",\n" +
" \"age\": 15\n" +
" },\n" +
" {\n" +
" \"sex\": \"女\",\n" +
" \"name\": \"李四\",\n" +
" \"age\": 16\n" +
" }\n" +
" ]\n" +
"}";
System.out.println(JSON.parse(str));//{"gradle":"高一","number":"2","people":[{"sex":"男","name":"张三","age":15},{"sex":"女","name":"李四","age":16}]}
Map<String,Object> map = JSON.parseObject(str, new TypeReference<Map<String, Object>>(){});
System.out.println(map);
System.out.println(map.get("gradle"));
System.out.println(map.get("number"));
System.out.println(map.get("people"));
String people = map.get("people").toString();
JSONArray objects = JSON.parseArray(people);
for(int i=0;i<objects.size();i++){
JSONObject jsonObject = objects.getJSONObject(i);
String name = jsonObject.getString("name");
Integer age = Integer.valueOf(jsonObject.getString("age"));
String sex = jsonObject.getString("sex");
Student s = new Student(name, age, sex);
String jsonString = JSON.toJSONString(s);
/**
* {"age":15,"name":"张三","sex":"男"}
* {"age":16,"name":"李四","sex":"女"}
*/
System.out.println(jsonString);
}
七、json数组转List
String str = "{\n" +
" \"gradle\": \"高一\",\n" +
" \"number\": \"2\",\n" +
" \"people\": [\n" +
" {\n" +
" \"sex\": \"男\",\n" +
" \"name\": \"张三\",\n" +
" \"age\": 15\n" +
" },\n" +
" {\n" +
" \"sex\": \"女\",\n" +
" \"name\": \"李四\",\n" +
" \"age\": 16\n" +
" }\n" +
" ]\n" +
"}";
JSONObject jsonObject = JSON.parseObject(str);
System.out.println(jsonObject);//{"gradle":"高一","number":"2","people":[{"sex":"男","name":"张三","age":15},{"sex":"女","name":"李四","age":16}]}
JSONArray people = jsonObject.getJSONArray("people");
//转list
List<JSONObject> javaList = people.toJavaList(JSONObject.class);
System.out.println(javaList);//[{"sex":"男","name":"张三","age":15}, {"sex":"女","name":"李四","age":16}]
javaList.forEach(json->{
//也可以在添加新的数据
Map<String, ? extends Serializable> stringMap = MapKit.mapOf("phone", 12232323, "address", "甘肃省");
json.putAll(stringMap);
System.out.println(json);
/**
* {"address":"甘肃省","phone":12232323,"sex":"男","name":"张三","age":15}
* {"address":"甘肃省","phone":12232323,"sex":"女","name":"李四","age":16}
*/
});
原文地址:https://blog.csdn.net/lpf11214/article/details/137549297
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!