[fastjson] - fastjson中 JSONObject 和 JSONArray
/**
* 对jsonObject对象进行key的获取
* @param jsonObject
*/ public ArrayList<String> jsonKeyRecursion(JSONObject jsonObject,ArrayList urlArr){
//创建urlArr用来存放url路径values
JSONObject littleJson = null; //对jsonObject中的url(key)的value进行更替, 加入授权请求连接
// String key = null;
for(String key : jsonObject.keySet()){
System.out.println("key : "+key);
//返回新的button下的json串
littleJson = (JSONObject) jsonObject.get(key);
System.out.println("littleJson : "+littleJson); if(key.equals("url")){
urlArr.add(littleJson);
System.out.println("add one littleJson to array : "+littleJson);
}
}
boolean url = littleJson.containsKey("");
System.out.println("url:"+url);
//这里的littleJson是JSONArray了,所以传入失败
jsonKeyRecursion(littleJson,urlArr); // Set<String> strings = littleJson.keySet();
//// littleJson
// System.out.println("strings:"+strings); // 1. button
// Set<Map.Entry<String, Object>> entries = littleJson.entrySet();
// Object[] objects = entries.toArray();
// System.out.println(objects);
// System.out.println("entries:"+entries); // 1. [button=[{"name":"今日歌曲","sub_button":[],"type":"click","key ...
// Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
// if(iterator.hasNext()){
// System.out.println("iterator.next():"+iterator.next());
// } return urlArr;
}
并没有获取复杂的嵌套json数据的很好的方法么
待解析
当第一次传入的jsonObject进行了get(key)之后,输出littleJson是去掉微信自定义菜单第一个key值(menu)后的button开头的json集合
但当要 get(button) 获取所剩下的button的集合时,程序提示出错为 JSONArray can't cast type to JSONObject .
使用强制转换失败,所以需要新的方法,让其(JSONArray)进入再一次迭代遍历中获取key的value,获取key的value直到把他们都装入键值对的map中.
{"menu":{"button":[{"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC","sub_button":[]},{"type":"click","name":"歌手简介","key":"V1001_TODAY_SINGER","sub_button":[]},{"name":"菜单","sub_button":[{"type":"view","name":"搜索","url":"http://www.ykmimi.com/","sub_button":[]},{"type":"view","name":"视频","url":"http://www.ykmimi.com/","sub_button":[]},{"type":"click","name":"赞一下我们","key":"V1001_GOOD","sub_button":[]}]}]}}
例如之上的这条json字符串.
[fastjson] - fastjson中 JSONObject 和 JSONArray的更多相关文章
- fastjson如何判断JSONObject和JSONArray
1.fastjson如何判断JSONObject和JSONArray,百度一下,教程还真不少,但是是阿里的fastjson的我是没有找到合适的方法.这里用一个还算可以的方法,算是实现了这个效果. 网上 ...
- [转]java中JSONObject与JSONArray的使用详细说明及有关JSON的工具类
JSONObject与JSONArray的使用 一.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: 1.commons-lang.jar 2.c ...
- java中JSONObject与JSONArray的使用
JSONObject与JSONArray 最近在学习过程中用到了稍微复杂点的json数据需要将json数据解析出来,这里就截取一部分作为例子 1.JSONObject介绍 JSONObject-lib ...
- JavaScript中JSONObject和JSONArray相关知识备忘(网络转载)
1.json的格式,有两种: {"key": "value"} //JSONObject(对象) [{"key1": "value ...
- FastJson中JSONObject用法及常用方法总结
本文为博主原创,未经允许不得转载: 最近一直有用到解析各种数据,主要是用FastJson进行数据解析,其中一个重要的类为JSONObject,今天有时间,所以进行总结一下: JSONobject是Fa ...
- Fastjson, Gson, org.json.JSON三者对于JSONObject及JSONArray的判断
1.Fastjson 我们通常在已知格式的情况下直接使用JSONObject,JSONArray,但是如果遇到需要判断格式呢? try{ Object object = JSON.parse(a); ...
- fastjson之JSONObject、JSONArray
JSONObject,JSONArray是JSON的两个子类. 首先我们来看JSONObject源码: 会发现JSONObject是继承Map<String, Object>,并且都是使用 ...
- java和js中JSONObject,JSONArray,Map,String之间转换
--------------------------------------------------Java中--------------------------------------------- ...
- 嵌套的JsonObject与JSONArray的取值---JSON中嵌套JSONArray
在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的.就跟if else语句一样,如果if中套if,if中再套if,写的 ...
随机推荐
- mouseover&mouseout和mouseenter&mouseleave
mouseenter&mouseleave: 进入被选元素触发,进入被选元素的子元素不会重复触发. mouseover&mouseout: 进入被选元素触发,从被选元素进入其子元素会再 ...
- html5实现的一些效果
一.网页换肤 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- PHP pdf转化为图片(PNG)
/** * 将pdf文件转化为多张png图片 * @param string $pdf pdf所在路径 (/www/pdf/abc.pdf pdf所在的绝对路径) * @param string $p ...
- [UML]UML 教程
统一建模语言(UML)已经迅速变成建立面向对象软件的事实标准.本教程提供了Enterprise Architect支持的13种UML图的技术概览.UML 2 详细的语义解释请看新的UML 2 教程. ...
- oracle如何四舍五入?
转自:http://www.jb51.net/article/84924.htm 取整(向下取整): 复制代码代码如下: select floor(5.534) from dual;select tr ...
- js数组转成对象
$scope.addalerts = []; $scope.addalertsString = JSON.stringify($scope.addalerts); 全部教程http://each.si ...
- c#字符串切割split使用方法
string strtest = "asdfg12wertgv1287654" "}, StringSplitOptions.RemoveEmptyEntries); 结 ...
- centos7 iptables/firewalld docker open port
here are multiple "hackish" ways to do it: scan kernel logs, as mentioned by Jiri (but you ...
- 小米范工具系列之六:小米范 web查找器2.x版本发布
小米范web查找器是一款快速识别端口及服务的小工具. 此工具使用java 1.8以上版本运行. 下载地址:http://pan.baidu.com/s/1c1NDSVe 文件名web finder ...
- centos vim编辑器 第八节课
centos vim编辑器 第八节课 其他编辑器: nanogeditemacs 腾讯云上的vim版本~ VIM - Vi IMproved ~ ~ version 7.4.629 ~ by Br ...