Java笔记-FastJson中JsonObject、JsonArray和JsonStr和JavaBean之间的互相转化
FastJson是阿里开源的JSON库Maven引入包依赖
com.alibaba
fastjson
1.2.37
JSONObject
【Java笔记-FastJson中JsonObject、JsonArray和JsonStr和JavaBean之间的互相转化】相互转化关系图
文章图片
- JavaBean
public class Person {
private int age;
private String name;
public int getAge() {return age;
}
public void setAge(int age) {this.age = age;
}
public String getName() {return name;
}
public void setName(String name) {this.name = name;
}
}
- JSONStr
{"age":20,"name":"xiaoming"}
- JSONObject
{
"age": 20,
"name": "xiaoming",
}
Demo
- JSONObject转JSONStr
public void test() {
JSONObject object = new JSONObject();
object.put("age", 20);
object.put("name", "xiaoming");
String str = JSON.toJSONString(object);
}
- JSONStr转JSONObject
public void test() {
String str = "{\"sex\":\"male\",\"name\":\"xiaoming\",\"age\":20}";
JSONObject object = JSON.parseObject(str);
}
- JSONStr转JavaBean
public void test() {
String str = "{\"sex\":\"male\",\"name\":\"xiaoming\",\"age\":20}";
Person person = JSON.parseObject(str, Person.class);
}
- JavaBean转JSONStr
public void test() {
Person person = new Person(20, "xiaoming");
String str = JSON.toJSONString(person);
}
- JavaBean转JSONObject
public void test() {
Person person = new Person(20, "xiaoming");
JSONObject object = (JSONObject) JSON.toJSON(person);
}
JSONArray
相互转化关系图
文章图片
- JavaBean
public class Person {
private int age;
private String name;
public int getAge() {return age;
}
public void setAge(int age) {this.age = age;
}
public String getName() {return name;
}
public void setName(String name) {this.name = name;
}
}
- JSONStr
[{"name":"xiaohua","age":18},{"name":"xiaoming","age":20}]
- JSONArray
[
{
"name": "xiaohua",
"age": 18
},
{
"name": "xiaoming",
"age": 20
}
]
Demo
- JSONArray转JSONStr
public void test() {
JSONObject object1 = new JSONObject();
object1.put("age", 20);
object1.put("name", "xiaoming");
JSONObject object2 = new JSONObject();
object2.put("age", 18);
object2.put("name", "xiaohua");
JSONArray array = new JSONArray();
array.add(object1);
array.add(object2);
String str = JSON.toJSONString(array);
}
- JSONStr转JSONArray
public void test() {
String str = "[{\"name\":\"xiaoming\",\"age\":20},{\"name\":\"xiaohua\",\"age\":18}]";
JSONArray array = JSON.parseArray(str);
}
- JSONStr转JavaBean-List
public void test() {
String str = "[{\"name\":\"xiaoming\",\"age\":20},{\"name\":\"xiaohua\",\"age\":18}]";
List personList = JSON.parseArray(str, Person.class);
}
- JavaBean-List转JSONStr
public void test() {
Person person1 = new Person(18, "xiaohua");
Person person2 = new Person(20, "xiaoming");
List personList = new ArrayList<>();
personList.add(person1);
personList.add(person2);
String str = JSON.toJSONString(personList);
}
- JavaBean-List转JSONArray
public void test() {
Person person1 = new Person(18, "xiaohua");
Person person2 = new Person(20, "xiaoming");
List personList = new ArrayList<>();
personList.add(person1);
personList.add(person2);
JSONArray array = (JSONArray) JSON.toJSON(personList);
}
推荐阅读
- 面试|实用 - Java后端面试经历(经验分享)适用于2~3年
- K8S学习笔记0522
- JavaEE|魔改一个自以为不太好的简历
- java|10道经典链表面试题
- Java道路简述
- 定时器
- 一文了解Java动态代理的原理及实现
- 笔记
- Java|Java 学习笔记_2
- javascript深拷贝的几种情况总结