嵌套的JsonObject与JSONArray的取值---JSON中嵌套JSONArray
在复杂的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的更多相关文章
- json字符串转JSONObject和JSONArray以及取值
import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JsonTest { public static v ...
- python re.findall(rule,data),根据左右边界取值url中参数的值
import re ''' 取值postid,左边界"postid=",右边界"&" ''' url="http://wwww.baidu.c ...
- jsonArray与 jsonObject区别与js取值
一.JSONObject和JSONArray的数据表示形式 JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...
- ComboboxColumn取值——Winform中DataGridView中某一列使用下拉框
ComboboxColumn的用法网上很多,绑定数据源都很简单,这里我遇到的是.不绑定数据源,即所有comobox的绑定都是固定的几个数据: 可以看到没有绑定任何数据源, ,在后台cs中取到下拉框的值 ...
- springboot中返回值json中null转换空字符串
在实际项目中,我们难免会遇到一些无值.当我们转JSON时,不希望这些null出现,比如我们期望所有的null在转JSON时都变成“”“”这种空字符串,那怎么做呢? Jackson中对null的处理 @ ...
- 循环取到json中的字段数据,加到html中
$.ajax({ type:'post', data:{specialName:specialName,count:count}, url:"admin/pcAdminGetArticleL ...
- 关于JSON的简介及取值以及常见面试题
关于JSON的简介及取值 JSON(JavaScript Object Notation)一种轻量级的数据交互格式 类似于一种数据封装,可以想象为java中student封装类 JSON的数值可以是数 ...
- python迭代器-迭代器取值-for循环-生成器-yield-生成器表达式-常用内置方法-面向过程编程-05
迭代器 迭代器 迭代: # 更新换代(其实也是重复)的过程,每一次的迭代都必须基于上一次的结果(上一次与这一次之间必须是有关系的) 迭代器: # 迭代取值的工具 为什么用迭代器: # 迭代器提供了一种 ...
- json中获取key值
<script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...
随机推荐
- glViewport函数用法
一. 其函数原型为glViewport(GLint x,GLint y,GLsizei width,GLsizei height) x,y 以像素为单位,指定了窗口的左下角位置. width,heig ...
- ffmpeg函数02__swr_alloc_set_opts()
SwrContext *swr_alloc(void); // 分配重采样的上下文. SwrContext *swr_alloc_set_opts(struct SwrContext *s, int ...
- win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结
win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结 一:前提 注意:现在有两种安装的方式 1. oracle11g服务端(64位)+oracle客户端(32位)+plsql(3 ...
- 高性能mysql 第1章 mysql架构与历史
mysql逻辑架构图: 第一层 客户端 第二层(服务层):针对所有类型的存储引擎可以公共提取的部分.将存储引擎抽离之后的其他部分都在这里.如:查询解析,分析优化,内置函数,存储过程,触发器,视图. 第 ...
- No module named 'winrandom'。
问题:在windows的python3使用PyCrypto出现ImportError: No module named 'winrandom'错误处理:修改python3安装目录下的 lib/Cry ...
- js array map() 函数的简单使用
语法: array.map(function(currentValue,index,arr), thisValue) currentValue:必须.当前元素的值 index:可选.当前元素的索引值 ...
- java和c中i++(i--)的区别(网易笔试2014)
java代码: public class Test{ public static void main(String[] args){ int a=5; int b=(a++)*(a++);//5*6自 ...
- [人物存档]【AI少女】【捏脸数据】现代风格
点击下载:6543d037acaf2edd03b4679f821001e9380193ce.png
- jquery focus()方法 语法
jquery focus()方法 语法 作用:当元素获得焦点时,发生 focus 事件.大理石平台价格 触发focus事件语法:$(selector).focus() 将函数绑定到focus事件语法: ...
- [EOJ Monthly2019.11][T1]纸条
https://acm.ecnu.edu.cn/ 华东师范大学在线评测网站 今天这个题目来自华东师范大学的校赛,比icpc稍难一些,在2019年11月29日周五19:30开始,持续2.5个小时 以下是 ...