=

为什么使用fastjson

引入fastjson

通过Maven引入fastjson

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>

fastjsonAPI介绍

工具类中

1.parse:字符串或者文本转JSON对象
2.toJSON…,Object对象转JSON
3.JSON对象的put添加方法
4.JSON对象的remove方法

public class JSONTest {
@Test
public void print() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("1", 1);
jsonObject.put("2", 2);
jsonObject.remove("3", 3);
User user = new User();
user.setUserName("Hello");
jsonObject.put("user", JSONObject.toJSON(user));
System.out.println(jsonObject);
String json=jsonObject.toJSONString();
JSONObject jsonObject1 = JSONObject.parseObject(json);
jsonObject1.put("123",1);
System.out.println(jsonObject1);
}
}
{"1":1,"2":2,"user":{"userName":"Hello"}}
{"1":1,"2":2,"123":1,"user":{"userName":"Hello"}}

大量解析JSON

大量解析格式化JSON不卡的地方:
​​​http://www.bejson.com/jsonviewernew/​