在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的。就跟if else语句一样,如果if中套if,if中再套if,写的规范了还行,要是代码格式不规范,那我们肯定也看着麻烦。所以啊,对于json嵌套,只要记住符号“:”前是键,符号后是值,大括号成对找,一层层剥开,就清楚了。 举个例子说明,如下:

{
"resultcode":"200",
"reason":"成功的返回",
"result":{
"company":"顺丰",
"com":"sf",
"no":"575677355677",
"list":[
{
"datetime":"2013-06-25 10:44:05",
"remark":"已收件",
"zone":"台州市"
},
{
"datetime":"2013-06-25 11:05:21",
"remark":"快件在 台州 ,准备送往下一站 台州集散中心 ",
"zone":"台州市"
}
],
"status":1
},
"error_code":0
}

获取方式

 package com.json;

 import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JsonObjectAndJsonArrayDemo {
public static void main(String[] args){
//复杂的json数据
String jsonStr = "{\"resultcode\":\"200\",\"reason\":\"成功的返回\",\"result\":{\"company\":\"顺丰\",\"com\":\"sf\","
+ "\"no\":\"575677355677\",\"list\":[{\"datetime\":\"2013-06-25 10:44:05\",\"remark\":\"已收件\",\"zone\":\"台州市\"},"
+ "{\"datetime\":\"2013-06-25 11:05:21\",\"remark\":\"快件在 台州 ,准备送往下一站 台州集散中心 \",\"zone\":\"台州市\"}],\"status\":1},"
+ "\"error_code\":0}";
JSONObject json = JSONObject.fromObject(jsonStr); //得到整个json串
System.out.println("resultcode:"+json.getString("resultcode")); //根据key得到value:200
System.out.println("reason:"+json.getString("reason")); //根据key得到value:成功的返回 //当遇到result时,也是将它当成一个整体串
System.out.println("company:"+JSONObject.fromObject(json.getString("result")).getString("company"));
System.out.println("com:"+JSONObject.fromObject(json.getString("result")).getString("com"));
System.out.println("no:"+JSONObject.fromObject(json.getString("result")).getString("no")); //当遇到再次嵌套时,此时的list是一个JSONArray,所以需要将其当作数组的形式处理,其实还是一个串
//get(i)以下标为主,里面的东西,其实也是一个json的形式数据,你可以不看外面怎么嵌套,按道理只当成单单一个json串处理即可
System.out.println("list(0).datetime:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(0)).getString("datetime")); System.out.println("list(0).remark:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(0)).getString("remark")); System.out.println("list(0).zone:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(0)).getString("zone")); System.out.println("list(1).datetime:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(1)).getString("datetime")); System.out.println("list(1).remark:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(1)).getString("remark")); System.out.println("list(1).zone:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(1)).getString("zone")); System.out.println("status:"+JSONObject.fromObject(json.getString("result")).getString("status")); System.out.println("reason:"+json.getString("error_code"));
}
}

摘自:JSON4:嵌套的JsonObject与JSONArray的取值

2017-12-07   09:57:13

嵌套的JsonObject与JSONArray的取值---JSON中嵌套JSONArray的更多相关文章

  1. json字符串转JSONObject和JSONArray以及取值

    import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JsonTest { public static v ...

  2. python re.findall(rule,data),根据左右边界取值url中参数的值

    import re ''' 取值postid,左边界"postid=",右边界"&" ''' url="http://wwww.baidu.c ...

  3. jsonArray与 jsonObject区别与js取值

    一.JSONObject和JSONArray的数据表示形式 JSONObject的数据是用 {  } 来表示的, 例如:   { "id" : "123", & ...

  4. ComboboxColumn取值——Winform中DataGridView中某一列使用下拉框

    ComboboxColumn的用法网上很多,绑定数据源都很简单,这里我遇到的是.不绑定数据源,即所有comobox的绑定都是固定的几个数据: 可以看到没有绑定任何数据源, ,在后台cs中取到下拉框的值 ...

  5. springboot中返回值json中null转换空字符串

    在实际项目中,我们难免会遇到一些无值.当我们转JSON时,不希望这些null出现,比如我们期望所有的null在转JSON时都变成“”“”这种空字符串,那怎么做呢? Jackson中对null的处理 @ ...

  6. 循环取到json中的字段数据,加到html中

    $.ajax({ type:'post', data:{specialName:specialName,count:count}, url:"admin/pcAdminGetArticleL ...

  7. 关于JSON的简介及取值以及常见面试题

    关于JSON的简介及取值 JSON(JavaScript Object Notation)一种轻量级的数据交互格式 类似于一种数据封装,可以想象为java中student封装类 JSON的数值可以是数 ...

  8. python迭代器-迭代器取值-for循环-生成器-yield-生成器表达式-常用内置方法-面向过程编程-05

    迭代器 迭代器 迭代: # 更新换代(其实也是重复)的过程,每一次的迭代都必须基于上一次的结果(上一次与这一次之间必须是有关系的) 迭代器: # 迭代取值的工具 为什么用迭代器: # 迭代器提供了一种 ...

  9. json中获取key值

    <script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...

随机推荐

  1. 斐波那契数列 Java 不同的实现方法所需要的时间比较

    # 首先我们直接看一个demo以及他的结果 public class QQ { public static void main(String[] args) throws ParseException ...

  2. 让IE8和IE9支持 placeholder

    1.原因:placeholder是h5的新属性,IE10以前的浏览器(8.9)不支持此属性. 2.解决方法:jQuery三方插件  jquery-placeholder 3.快速开始: <!DO ...

  3. Python 操作 MySQL 数据库Ⅱ

    数据库连接 连接数据库前,请先确认以下事项: 您已经创建了数据库 TESTDB. 在TESTDB数据库中您已经创建了表 EMPLOYEE EMPLOYEE表字段为 FIRST_NAME, LAST_N ...

  4. Release和Debug的区别

    Debug与Release版本的区别 Debug 和 Release 并没有本质的区别,他们只是VC预定义提供的两组编译选项的集合,编译器只是按照预定的选项行动.如果我们愿意,我们完全可以把Debug ...

  5. NOI 2019 AFO 记

    Text 真的退役了... 非常抱歉 这篇文章暂时咕掉了

  6. node.js实现web解析dns

    var http = require('http'), //服务器创建 dns = require('dns'), //DNS查询,主要负责解析当前DNS域名,返回DNS服务器IP地址 fs = re ...

  7. php 将几个变量合为数组,变量名和值对应

    <?php $firstname = "Bill"; $lastname = "Gates"; $age = "60"; $resul ...

  8. JavaWeb_(SSH论坛)_六、点赞模块

    基于SSH框架的小型论坛项目 一.项目入门 传送门 二.框架整合 传送门 三.用户模块 传送门 四.页面显示 传送门 五.帖子模块 传送门 六.点赞模块 传送门 七.辅助模块 传送门 联合主键 创建p ...

  9. JS框架_(JQuery.js)网页文字评论弹幕

    百度云盘 传送门 密码:3azl jQuery网页右下角文字评论弹幕效果 <!DOCTYPE html> <html> <head> <title>jQ ...

  10. jQuery实现表单动态添加与删除数据操作示例

    <!DOCTYPE html> <html lang="en"> <head>   <meta charset="UTF-8&q ...