package com.example.demo.json;

 import java.util.Map;

 import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.demo.common.Person; public class JsonLib {
//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}]}";
@SuppressWarnings("unchecked")
public static void main(String[] args) {
//demoJson(); //testJSONStrToJSONObject();//json字符串转化对象
//testJSONStrToJSONArray();//json数组转化json对象
testComplexJSONStrToJSONObject();//json对象嵌套json对象
} /**
* 复杂json格式字符串与JSONObject之间的转换
*/
public static void testComplexJSONStrToJSONObject(){
System.out.println(COMPLEX_JSON_STR);
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的
System.out.println(jsonObject);
String teacherName = jsonObject.getString("teacherName");
Integer teacherAge = jsonObject.getInteger("teacherAge");
JSONObject course = jsonObject.getJSONObject("course");
JSONArray students = jsonObject.getJSONArray("students");
System.out.println(teacherName+"------"+teacherAge+"===json对象===="+course+"----json数组----"+students);
JSONArray jsonArray = JSON.parseArray(students.toString());
System.out.println(jsonArray);
} /**
* json字符串-数组类型与JSONArray之间的转换
*/
public static void testJSONStrToJSONArray(){ JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
//JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因为JSONArray继承了JSON,所以这样也是可以的 //遍历方式1
int size = jsonArray.size();
for (int i = 0; i < size; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
} //遍历方式2
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
} /**
* json字符串-简单对象型与JSONObject之间的转换
*/
public static void testJSONStrToJSONObject(){ JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因为JSONObject继承了JSON,所以这样也是可以的 System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge")); }
public static void demoJson() {
/**
* 将 Json 形式的字符串转换为 Map
*/
String str = "{\"name\":\"Tom\",\"age\":90}";
JSONObject jsonObject = JSONObject.parseObject(str);
Map<String, String> params = JSONObject.parseObject(jsonObject.toString(), new TypeReference<Map<String, String>>(){});
System.out.println(params); /**
* 将 Json 形式的字符串转换为 JavaBean
*/
str = "{\"id\":\"A001\",\"name\":\"Jack\"}";
jsonObject = JSONObject.parseObject(str);
System.out.println(jsonObject);
Person person = JSON.parseObject(str, new TypeReference<Person>() {});
System.out.println(person.toString());
}
}

给大家推荐一个很好的自学网站,https://how2j.cn?p=77721,how2j,从基础到项目,一应俱全。可以先注册再学习,这样就可以记录学习进度咯!!!

最新文章

  1. MTP in Android详解
  2. glusterfs 内存管理方式
  3. ruby 2.2
  4. ASP.NET MVC使用Bootstrap系列(1)——开始使用Bootstrap
  5. 【cs229-Lecture19】微分动态规划
  6. 聊聊 iOS 开发中的协议
  7. jQuery 标签淡入淡出 个人随笔
  8. 检查java class的版本号
  9. java中equals方法和hashcode方法的区别和联系,以及为什么要重写这两个方法,不重写会怎样
  10. HTTP协议扫盲(二)HTTP协议的请求方法、请求头和响应头
  11. (一)UI设计的一些常识
  12. netty-socketio 概述
  13. 步步为营-64-进程&amp;线程
  14. Highcharts 柱图 每个柱子外围的白色边框
  15. List集合中的数据按照某一个属性进行分组
  16. Nodejs异步框架——async
  17. Java写的数据库连接池
  18. 《你不知道的JavaScript》系列分享专栏
  19. Django 2.0 学习(18):Django 缓存、信号和extra
  20. Vue.js经典开源项目汇总

热门文章

  1. AtCoder AGC038 C-LCMs 题解
  2. 洛谷P1064 金明的预算方案(01背包)
  3. vs2019 opencv4的相关配置
  4. netty(五) channel
  5. canvas的beginPath和closePath
  6. @Primary 注解的作用
  7. phantomjs安装步骤
  8. IDEA导入maven项目不自动识别
  9. acm数论之旅(转载) -- 快速幂
  10. SSH 维持权限(好用)