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,从基础到项目,一应俱全。可以先注册再学习,这样就可以记录学习进度咯!!!

Java解析json数组三种情况的更多相关文章

  1. java解析xml的三种方法

    java解析XML的三种方法 1.SAX事件解析 package com.wzh.sax; import org.xml.sax.Attributes; import org.xml.sax.SAXE ...

  2. JAVA写JSON的三种方法,java对象转json数据

    JAVA写JSON的三种方法,java对象转json数据 转自:http://www.xdx97.com/#/single?bid=5afe2ff9-8cd1-67cf-e7bc-437b74c07a ...

  3. java解析json数组

      java解析json数组 import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; ...

  4. go 中解析JSON的三种姿势

    背景 这是一篇写给0-1年新人的文章,短平快的教会你如何解析json字符串. 示例Json 假设有如下json字符串: { "userName":"admin" ...

  5. 解析JSON的三种方式

    JSONObject   JSONObject jsonObject = new JSONObject(strJson); JSONArray jsonArray = jsonObject.getJS ...

  6. JSON的三种解析方式

    一.什么是JSON? JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度. JSON就是一串字符串 只不过元素会使用特定 ...

  7. Java解析json字符串和json数组

    Java解析json字符串和json数组 public static Map<String, String> getUploadTransactions(String json){ Map ...

  8. Java实现PV操作 | 读者与写者(在三种情况下进行讨论)

    注 :本文应结合[天勤笔记]进行学习. 1.读者优先 设置rmutex信号量来对readcount变量进行互斥访问.mutex信号量对写者与读者进行同步. static syn rmutex=new ...

  9. Introduction to Structured Data json的2种形式 JAVA解析JSON数据 - JsonArray JsonObject

    https://developers.google.com/search/docs/guides/intro-structured-data Structured data refers to kin ...

随机推荐

  1. uni-app 去除顶部导航栏

    自学uni-app第一天,因为有一点点的小程序和vue的基础所以感觉对uni-app有一点点的亲切感,从今天呢开始着手从登录页学习uni-app,记录一些用到的知识点,欢迎大家一起学习. 启动页隐藏顶 ...

  2. Fluent_Python_Part1序幕,01-data-model, 数据模型

    01-data-model/frenchdeck.py 1. Python解释器碰到特殊的句法时,会使用__特殊方法__去激活一些基本的对象操作. 特殊方法的存在是为了被解释器用的.没有my_obje ...

  3. Mysql与PostgreSql数据库学习笔记

    mysql 从最基础的数据引擎,到进程结构,都不能支持数据版本.导致其职能阻塞“并发”,不支持最基本的事务,innodb达不到基本事务要求,任何写数据,都导致整个表锁住.充其量只能算是一个玩具,或者说 ...

  4. 安卓开发:Android Studio自动import

    我只想说,真好用!哈哈,提高效率的好东西. 参考: [https://blog.csdn.net/pjdd123/article/details/80953669] [https://www.cnbl ...

  5. ajax传map,后端接收并解析

    前端let map = new Map(); map.set(1, 1); map.set(2, 2); map.set(3, 3); //map转obj let obj= Object.create ...

  6. JS-防抖与节流

    问题的由来:一些事件频繁的被触发而导致频繁的调用事件处理程序,从而造成程序不必要的开销,影响程序性能:防抖和节流就是为了解决这种情况造成的性能消耗. 场景1:使用keyup事件监听输入框的值进行请求搜 ...

  7. python opencv:图像的一些属性与操作

    img = cv.imread(xxx) # 常用的有以下属性 type(img) # img的数据类型 img.shape # img的结构 img.size # img的大小 img.dtype ...

  8. 基于scikitlearn的深度学习环境安装(三)(完整版)

    OS Linux  Ubuntu14.04 安装 pip (python2.7.9或以上自带pip) sudo apt-get install python-pip pip是python环境下安装包的 ...

  9. 【代码审计】VAuditDemo SQL注入漏洞

    这里我们定位 sqlwaf函数 在sys/lib.php中,过滤了很多关键字,但是42 43 44行可以替换为空 比如我们可以 uni||on来绕过过滤

  10. spark报错 java.lang.NoClassDefFoundError: scala/xml/MetaData

    代码: 报错信息: java.lang.NoClassDefFoundError: scala/xml/MetaData 原因:确失jar包 <dependency> <groupI ...