在复杂的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. Stopwatch简单时间检测

    public ActionResult Index() { Stopwatch sw = new Stopwatch(); //实例化一个对象 sw.Start(); //开始计算 int[] a = ...

  2. [CF 1238F] The Maximum Subtree 树DP

    题意 给定一颗树,求这个树的最大子树,且这个子树是一个good-tree. good-tree的定义是:每个节点可以表示成一个数值区间,而树上的边表示两个点表示的数值区间相交. 题解 通过分析可以发现 ...

  3. eclipse编码格式(中文乱码)

    https://jingyan.baidu.com/article/2009576193ee38cb0721b416.html 修改工作空间默认编码 1 进入Eclipse,导入一个项目工程,如果项目 ...

  4. springboot jpa 创建数据库以及rabbitMQ分模块扫描问题

    在使用jpa过程中,如果没有在配置中加入自动创建实体对于的sql,则需要提前创建建表语句 spring.jpa.properties.hibernate.show_sql=true spring.jp ...

  5. nginx反向代理原理及配置详解

    nginx概述nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外n ...

  6. 【leetcode】1232. Check If It Is a Straight Line

    题目如下: You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coord ...

  7. Java中SSM+Shiro系统登录验证码的实现方法

    1.验证码生成类: import java.util.Random; import java.awt.image.BufferedImage; import java.awt.Graphics; im ...

  8. [转] SSH两种登录方式(公私钥)解析

    转自:https://www.cnblogs.com/hukey/p/6248468.html SSH登录方式主要分为两种: 1. 用户名密码验证方式 说明: (1) 当客户端发起ssh请求,服务器会 ...

  9. yiele函数

    1.概念: 当调用Thread.yield()函数时,会给线程调度器一个当前线程愿意让出CPU使用的暗示,但是线程调度器可能会忽略这个暗示. 2.实战. 看下面例子 public class yiel ...

  10. CF1155 E.Guess the Root

    题目链接:Click here 题目大意:现在有一个至多11项的多项式\(F(x)\),你可以询问至多50个\(x\),黑盒子会告诉你\(F(x)\)的值,你现在要找到一个\(x\)使得\(F(x)= ...