jsonobject 遍历 org.json.JSONObject
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public static void main(String[] args) {
- String str = "{'TI':[{'value':'aa1','count':10},{'value':'aa2','count':15},{'value':'aa3','count':20}]," +
- "'AB':[{'value':'ab','count':110},{'value':'ab2','count':115},{'value':'ab3','count':210}]}";
- JSONArray newArray = new JSONArray();
- JSONObject newJson = new JSONObject();
- try {
- JSONObject obj = new JSONObject(str);
- Iterator it = obj.keys();
- while (it.hasNext()) {
- String key = (String) it.next();
- String value = obj.getString(key);
- JSONArray array = obj.getJSONArray(key);
- for(int i=0;i<array.length();i++){
- JSONObject jsonobject = array.getJSONObject(i);
- jsonobject.put("name", key);
- jsonobject.put("exp", key+"="+jsonobject.getString("value"));
- newArray.put(jsonobject);
- }
- }
- newJson.put("groups",newArray);
- System.out.println(newJson);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- {"groups":[{"exp":"AB=ab","count":110,"name":"AB","value":"ab"},{"exp":"AB=ab2","count":115,"name":"AB","value":"ab2"},{"exp":"AB=ab3","count":210,"name":"AB","value":"ab3"},{"exp":"TI=aa1","count":10,"name":"TI","value":"aa1"},{"exp":"TI=aa2","count":15,"name":"TI","value":"aa2"},{"exp":"TI=aa3","count":20,"name":"TI","value":"aa3"}]}
jsonobject 遍历 org.json.JSONObject的更多相关文章
- net.sf.json.JSONObject 和org.json.JSONObject 的差别
http://my.oschina.net/wangwu91/blog/340721 net.sf.json.JSONObject 和org.json.JSONObject 的差别. 一.创建jso ...
- net.sf.json.JSONObject 和org.json.JSONObject
参考 net.sf.json.JSONObject 和org.json.JSONObject 的差别
- JsonArray和JsonObject遍历方法
一:遍历JsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}, ...
- 遍历jsonArray和jsonObject
遍历jsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}]&q ...
- JSONObject遍历
导入JSONObject和JSONArray所需要的jar包 JSONObject所必需的6个jar包: commons-beanutils-1.7.0.jar commons-collections ...
- 使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject
先是报 java.lang.NoClassDefFoundError: net/sf/json/JSONObject 这个错误, 打开项目属性找到java build path中的libaries,找 ...
- 大话 JSON 之 JSONObject.getString(“”) 方法 和 JSONObject.optString(“”) 的区别
运行以下代码: public static void main(String[] args) { JSONObject test = new JSONObject(); test.put(" ...
- java中转换json方式(JSONArray,JSONObject),json解析
package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.Has ...
- org.json.JSONObject的getString和optString使用注意事项
结论:org.json.JSONObject的getString如果取不到对应的key会抛出异常,optString则不会 /** * Returns the value mapped by {@co ...
随机推荐
- Wix安装程序中判断是否安装的.net framwork 4.5
<PropertyRef Id="NETFRAMEWORK40FULL"/> <PropertyRef Id="NETFRAMEWORK45" ...
- MySQL Performance tuning
1.表级锁状态 mysql> show status like 'table%'; +----------------------------+-----------+ | Variable_n ...
- iOS开发-canOpenURL: failed for URL: "xx" - error:"This app is not allowed to query for scheme xx"
转载自:http://www.jianshu.com/p/e38a609f786e
- [android]AndroidInject框架——我的第一个android小型框架
作为一个移动应用开发者,随着需求的日益增多,Android项目的越来越臃肿,代码量越来越大, 现在冷静下来回头看看我们的代码,有多少代码跟业务逻辑没什么关系的 所以,本人自不量力,在github上建了 ...
- yii 项目根目录下需要有assets目录
yii 项目根目录下需要有assets目录,如果没有,会导致gii失效
- 对URL编码
url支持26个英文字母.数字和少数几个特殊字符,因此,对于url中包含非标准url的字符时,就需要对其进行编码.iOS中提供了函数stringByAddingPercentEscapesUsingE ...
- 【代码笔记】iOS-显示图片的各种方式
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UI ...
- Android开发框架--AndroidAnnotations(一)
annotation:注解 什么是框架 框架是基石 android开发中哪些内容可以做成框架呢 网络模块 图片缓存模块 数据库模块 UI基础模块 开发框架给我们带来了哪些好处 提升开发效率 代码简洁 ...
- js 变量与值 连写
window.location.href="index.php?app=memberpmrecord&act=get_download&bname="+busine ...
- js实现页面跳转的几种方式
第一种: <script language="javascript" type="text/javascript"> wi ...