FastJson

FastJson使用


比较关注的是JSON对象和JSON字符串之间的转换

1.FastJson特点

  1. FastJson数度快,无论序列化和反序列化,都是当之无愧的fast
  2. 功能强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)
  3. 零依赖(没有依赖其它任何类库)

2.FastJson的三个类

  1. JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换
  2. JSONObject:fastJson提供的json对象
  3. JSONArray:fastJson提供json数组对象

3.JSON格式字符串和JSON对象之间转换

//json字符串-简单对象型
private static final String  JSON_OBJ_STR = "{"studentName":"lily","studentAge":12}";

//json字符串-数组类型
private static final String  JSON_ARRAY_STR = "[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]";

//复杂格式json字符串
private static final String  COMPLEX_JSON_STR = "{"teacherName":"crystall","teacherAge":27,"course":{"courseName":"english","code":1270},"students":[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]}";

3.0统一通用的相互转换

//记得这两条代码,可以不管一下的分类转换
JSONObject jsonObject = JSON.parseObject(JSON_STR);
Strign jsonStr = JSON.toJSONString(jo);

3.1 json字符串-简单对象型与JSONObject之间的转换

JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
String jsonString = JSONObject.toJSONString(jsonObject);

3.2 json字符串(数组类型)与JSONArray之间的转换

JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
String jsonString = JSONArray.toJSONString(jsonArray);

3.3 复杂json格式字符串与JSONObject之间的转换

JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
String jsonString = JSONObject.toJSONString(jsonObject);
原文地址:https://www.cnblogs.com/nadech/p/9268266.html