JAVA 最全最细的fastjson使用介绍,带你透彻领悟JSON

10,450次阅读
没有评论

共计 5410 个字符,预计需要花费 14 分钟才能阅读完成。

JSON 协议使用方便,越来越流行,JSON 的处理器有很多, 这里我介绍一下 FastJson,FastJson 是阿里的开源框架, 被不少企业使用, 是一个极其优秀的 Json 框架,Github 地址: FastJson

1.2.FastJson 的特点:

1.FastJson 数度快, 无论序列化和反序列化, 都是当之无愧的 fast

2. 功能强大(支持普通 JDK 类包括任意 Java Bean Class、Collection、Map、Date 或 enum)

3. 零依赖(没有依赖其它任何类库)

1.3.FastJson 的简单说明:

FastJson 对于 json 格式字符串的解析主要用到了下面三个类:

1.JSON:fastJson 的解析器,用于 JSON 格式字符串与 JSON 对象及 javaBean 之间的转换

2.JSONObject:fastJson 提供的 json 对象

3.JSONArray:fastJson 提供 json 数组对象

的东东,利人利己~

2.FastJson 的用法


首先定义三个 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}]}”;

// 可以打印看看,都是写啥

System.out.println(“—————————json 字符串 - 简单对象型 ——————————————–”);

System.out.println(“JSON_OBJ_STR:”+“n”+JSON_OBJ_STR);

System.out.println(“—————————json 字符串 - 数组类型 ———————————————-”);

System.out.println(“JSON_ARRAY_STR:”+“n”+JSON_ARRAY_STR);

System.out.println(“————————— 复杂格式 json 字符串 ———————————————–”);

System.out.println(“COMPLEX_JSON_STR:”+“n”+COMPLEX_JSON_STR);

2.1.JSON 格式字符串与 JSON 对象之间的转换

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

/**

  • json 字符串 - 简单对象型到 JSONObject 的转换

*/

@Test

public void testJSONStrToJSONObject() {

JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);

System.out.println(“studentName: ” + jsonObject.getString(“studentName”) +“:”+ ” studentAge: “

  • jsonObject.getInteger(“studentAge”));

}

/**

  • JSONObject 到 json 字符串 - 简单对象型的转换

*/

@Test

public void testJSONObjectToJSONStr() {

// 已知 JSONObject, 目标要转换为 json 字符串

JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);

// 第一种方式

String jsonString = JSONObject.toJSONString(jsonObject);

// 第二种方式

//String jsonString = jsonObject.toJSONString();

System.out.println(jsonString);

}

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

/**

  • json 字符串 - 数组类型到 JSONArray 的转换

*/

@Test

public void testJSONStrToJSONArray() {

JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);

// 遍历方式 1

int size = jsonArray.size();

for (int i = 0; i

JSONObject jsonObject = jsonArray.getJSONObject(i);

System.out.println(“studentName: ” + jsonObject.getString(“studentName”) +“:”+ ” studentAge: “

  • jsonObject.getInteger(“studentAge”));

}

// 遍历方式 2

for (Object obj : jsonArray) {

JSONObject jsonObject = (JSONObject) obj;

System.out.println(“studentName: ” + jsonObject.getString(“studentName”) +“:”+ ” studentAge: “

  • jsonObject.getInteger(“studentAge”));

}

}

/**

  • JSONArray 到 json 字符串 - 数组类型的转换

*/

@Test

public void testJSONArrayToJSONStr() {

// 已知 JSONArray, 目标要转换为 json 字符串

JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);

// 第一种方式

String jsonString = JSONArray.toJSONString(jsonArray);

// 第二种方式

//String jsonString = jsonArray.toJSONString(jsonArray);

System.out.println(jsonString);

}

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

/**

  • 复杂 json 格式字符串到 JSONObject 的转换

*/

@Test

public void testComplexJSONStrToJSONObject() {

JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);

String teacherName = jsonObject.getString(“teacherName”);

Integer teacherAge = jsonObject.getInteger(“teacherAge”);

System.out.println(“teacherName: ” + teacherName + ” teacherAge: ” + teacherAge);

JSONObject jsonObjectcourse = jsonObject.getJSONObject(“course”);

// 获取 JSONObject 中的数据

String courseName = jsonObjectcourse.getString(“courseName”);

Integer code = jsonObjectcourse.getInteger(“code”);

System.out.println(“courseName: ” + courseName + ” code: ” + code);

JSONArray jsonArraystudents = jsonObject.getJSONArray(“students”);

// 遍历 JSONArray

for (Object object : jsonArraystudents) {

JSONObject jsonObjectone = (JSONObject) object;

String studentName = jsonObjectone.getString(“studentName”);

Integer studentAge = jsonObjectone.getInteger(“studentAge”);

System.out.println(“studentName: ” + studentName + ” studentAge: ” + studentAge);

}

}

/**

  • 复杂 JSONObject 到 json 格式字符串的转换

*/

@Test

public void testJSONObjectToComplexJSONStr() {

// 复杂 JSONObject, 目标要转换为 json 字符串

JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);

// 第一种方式

//String jsonString = JSONObject.toJSONString(jsonObject);

// 第二种方式

String jsonString = jsonObject.toJSONString();

System.out.println(jsonString);

}

2.2.JSON 格式字符串与 javaBean 之间的转换

2.2.1.json 字符串 - 简单对象型与 javaBean 之间的转换

/**

  • json 字符串 - 简单对象到 JavaBean 之间的转换

*/

@Test

public void testJSONStrToJavaBeanObj() {

// 第一种方式

JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);

String studentName = jsonObject.getString(“studentName”);

Integer studentAge = jsonObject.getInteger(“studentAge”);

//Student student = new Student(studentName, studentAge);

// 第二种方式, 使用 TypeReference 类, 由于其构造方法使用 protected 进行修饰, 故创建其子类

//Student student = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference() {});

// 第三种方式, 使用 Gson 的思想

Student student = JSONObject.parseObject(JSON_OBJ_STR, Student.class);

System.out.println(student);

}

/**

  • JavaBean 到 json 字符串 - 简单对象的转换

*/

@Test

public void testJavaBeanObjToJSONStr() {

Student student = new Student(“lily”, 12);

String jsonString = JSONObject.toJSONString(student);

System.out.println(jsonString);

}

2.2.2.json 字符串 - 数组类型与 javaBean 之间的转换

/**

  • json 字符串 - 数组类型到 JavaBean_List 的转换

*/

@Test

public void testJSONStrToJavaBeanList() {

总结

面试建议是,一定要自信,敢于表达,面试的时候我们对知识的掌握有时候很难面面俱到,把自己的思路说出来,而不是直接告诉面试官自己不懂,这也是可以加分的。

以上就是蚂蚁技术四面和 HR 面试题目,以下最新总结的最全,范围包含最全 MySQL、Spring、Redis、JVM 等最全面试题和答案,仅用于参考

![一份还热乎的蚂蚁金服面经(已拿 Offer)面试流程 4 轮技术面 + 1 轮 HR](https://upload-images.jianshu.io/upload_images/24616006-22b51f9b8e9edfab?i 需要 zi 料 + 绿色徽【vip1024b】

mageMogr2/auto-orient/strip|imageView2/2/w/1240)

原文地址: JAVA 最全最细的 fastjson 使用介绍,带你透彻领悟 JSON

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于2024-10-07发表,共计5410字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)