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. awk函数实现将点分式形式的掩码转换为十进制形式的掩码

    在用awk处理文本时,需要将源数据为点分式形式的掩码转换为十进制形式的掩码,下边的函数可简单实现: 转换功能函数cdr2mask function cdr2mask(mask_point) { num ...

  2. Java中正负数的存储方式-正码 反码和补码

    Java中正负数的存储方式-正码 反码和补码 正码 我们以int 为例,一个int占用4个byte,32bits 0 存在内存上为 00000000 00000000 00000000 0000000 ...

  3. css中定义变量

    css中定义变量 定义变量可分多种情况: 1.定义全局变量 :root { --borderColor: #ccc;} 2.定义某元素下的变量 .look{ --borderColor: #ccc;} ...

  4. Travel in desert

    传送门 不算难吧 应该有思路的 还是太水了吧 (而且和货车运输很像的啊 ---------------------------------------------------------------- ...

  5. Jmeter BeanShell笔记

    前言:beanshell是支持java语法的,因此当一些复杂的数据结构jmeter处理不了的时候,我们可以借助于java来实现 1,使用beanshell处理JDBC请求返回的值 数据库结构 当JDB ...

  6. html表单中的input元素的两种提交方式比较(get/post)

    Http存在两种最常用的提交方式:Get和Post(电话面试有问到两种提交方式的区别) 什么是HTTP? 超文本传输协议(HTTP)的设计目的是保证客户机与服务器之间的通信. HTTP 的工作方式是客 ...

  7. Yii2.0 引入外部js css

    <script src="<?= Yii::$app->request->baseUrl . '/js/jquery-2.1.1.min.js'?>" ...

  8. 洛谷 P5018 对称二叉树(搜索)

    嗯... 题目链接:https://www.luogu.org/problem/P5018 其实这道题直接搜索就可以搜满分: 首先递归把每个点作为根节点的儿子的数量初始化出来,然后看这个节点作为根节点 ...

  9. http的长连接和短连接(史上最通俗!)

    1.以前的误解 很久之前就听说过长连接的说法,而且还知道HTTP1.0协议不支持长连接,从HTTP1.1协议以后,连接默认都是长连接.但终究觉得对于长连接一直懵懵懂懂的,有种抓不到关键点的感觉. 今天 ...

  10. 应用 AddressSanitizer 发现程序内存错误

    作为 C/ C++ 工程师,在开发过程中会遇到各类问题,最常见便是内存使用问题,比如,越界,泄漏.过去常用的工具是 Valgrind,但使用 Valgrind 最大问题是它会极大地降低程序运行的速度, ...