1. import org.json.JSONArray;
  2. import org.json.JSONException;
  3. import org.json.JSONObject;
  1. public static void main(String[] args) {
  2. String str = "{'TI':[{'value':'aa1','count':10},{'value':'aa2','count':15},{'value':'aa3','count':20}]," +
  3. "'AB':[{'value':'ab','count':110},{'value':'ab2','count':115},{'value':'ab3','count':210}]}";
  4. JSONArray newArray = new JSONArray();
  5. JSONObject newJson = new JSONObject();
  6. try {
  7. JSONObject obj = new JSONObject(str);
  8. Iterator it = obj.keys();
  9. while (it.hasNext()) {
  10. String key = (String) it.next();
  11. String value = obj.getString(key);
  12. JSONArray array = obj.getJSONArray(key);
  13. for(int i=0;i<array.length();i++){
  14. JSONObject jsonobject = array.getJSONObject(i);
  15. jsonobject.put("name", key);
  16. jsonobject.put("exp", key+"="+jsonobject.getString("value"));
  17. newArray.put(jsonobject);
  18. }
  19. }
  20. newJson.put("groups",newArray);
  21. System.out.println(newJson);
  22. } catch (JSONException e) {
  23. e.printStackTrace();
  24. }
  25. }
  1. {"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的更多相关文章

  1. net.sf.json.JSONObject 和org.json.JSONObject 的差别

    http://my.oschina.net/wangwu91/blog/340721 net.sf.json.JSONObject 和org.json.JSONObject  的差别. 一.创建jso ...

  2. net.sf.json.JSONObject 和org.json.JSONObject

    参考 net.sf.json.JSONObject 和org.json.JSONObject 的差别

  3. JsonArray和JsonObject遍历方法

    一:遍历JsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}, ...

  4. 遍历jsonArray和jsonObject

    遍历jsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}]&q ...

  5. JSONObject遍历

    导入JSONObject和JSONArray所需要的jar包 JSONObject所必需的6个jar包: commons-beanutils-1.7.0.jar commons-collections ...

  6. 使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject

    先是报 java.lang.NoClassDefFoundError: net/sf/json/JSONObject 这个错误, 打开项目属性找到java build path中的libaries,找 ...

  7. 大话 JSON 之 JSONObject.getString(“”) 方法 和 JSONObject.optString(“”) 的区别

    运行以下代码: public static void main(String[] args) { JSONObject test = new JSONObject(); test.put(" ...

  8. java中转换json方式(JSONArray,JSONObject),json解析

    package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.Has ...

  9. org.json.JSONObject的getString和optString使用注意事项

    结论:org.json.JSONObject的getString如果取不到对应的key会抛出异常,optString则不会 /** * Returns the value mapped by {@co ...

随机推荐

  1. Wix安装程序中判断是否安装的.net framwork 4.5

    <PropertyRef Id="NETFRAMEWORK40FULL"/> <PropertyRef Id="NETFRAMEWORK45" ...

  2. MySQL Performance tuning

    1.表级锁状态 mysql> show status like 'table%'; +----------------------------+-----------+ | Variable_n ...

  3. iOS开发-canOpenURL: failed for URL: "xx" - error:"This app is not allowed to query for scheme xx"

    转载自:http://www.jianshu.com/p/e38a609f786e

  4. [android]AndroidInject框架——我的第一个android小型框架

    作为一个移动应用开发者,随着需求的日益增多,Android项目的越来越臃肿,代码量越来越大, 现在冷静下来回头看看我们的代码,有多少代码跟业务逻辑没什么关系的 所以,本人自不量力,在github上建了 ...

  5. yii 项目根目录下需要有assets目录

    yii 项目根目录下需要有assets目录,如果没有,会导致gii失效

  6. 对URL编码

    url支持26个英文字母.数字和少数几个特殊字符,因此,对于url中包含非标准url的字符时,就需要对其进行编码.iOS中提供了函数stringByAddingPercentEscapesUsingE ...

  7. 【代码笔记】iOS-显示图片的各种方式

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UI ...

  8. Android开发框架--AndroidAnnotations(一)

    annotation:注解 什么是框架 框架是基石 android开发中哪些内容可以做成框架呢 网络模块 图片缓存模块 数据库模块 UI基础模块 开发框架给我们带来了哪些好处 提升开发效率 代码简洁 ...

  9. js 变量与值 连写

    window.location.href="index.php?app=memberpmrecord&act=get_download&bname="+busine ...

  10. js实现页面跳转的几种方式

    第一种:    <script language="javascript" type="text/javascript">           wi ...