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. 【转】SharePoint camel query查询 event 或者Calendar 日历列表的时候,怎么查询所有的重复发生的事件

    When you query a SharePoint calendar your results will contain: All non-recurring events The first e ...

  2. atitit.userService 用户系统设计 v4 q316 .doc

    atitit.userService 用户系统设计 v4 q316 .doc 1. 新特性1 2. Admin  login1 3. 用户注册登录2 3.1. <!-- 会员注册使用 --> ...

  3. Vault插件示例--Vault Explorer与Thin Client的集成。

    Autodesk Vault 2014的Subscription 包中有一个组件叫做Thin Client.这个瘦客户端有着全新的界面,又给了我们一个全新的选择.ThinClient实际是在Vault ...

  4. mysql innodb 奔溃问题

    ps -A | grep -i mysql kill 列出来的进程 service mysql start 我的问题就解决了 ------------------------------------- ...

  5. iOS开发中常用的单例

    定义:一个类的对象,无论在何时创建.无论创建多少次,创建出来的对象都是同一个对象. 使用场景:当有一些数据需要共享给别的类的时候,就可以把这些数据保存在单例对象中.   关键代码: + (instan ...

  6. entityframework lamda 使用where时的注意事项

    我在项目中做了个底层 访问数据库泛型类 BaseEFDao<T> 在获取实体模型的时候使用了 Entities.CreateObjectSet<T>().Where(Func& ...

  7. 【读书笔记】iOS-GCD-GCD与perfomSelector系方法比较

    一,GCD是导师步执行任务的技术之一.一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适当的Dispatch Queue中,CGD就能生成必要的线程并计划执行 ...

  8. Jexus-5.6.3使用详解、Jexus Web Server配置

    一.Jexus Web Server配置   在 jexus 的工作文件夹中(一般是“/usr/jexus”)有一个基本的配置文件,文件名是“jws.conf”. jws.conf 中至少有 Site ...

  9. 初识mvc分层思想

    首先要清楚的是: mvc是一种设计模式,一种分层思想,没有具体的技术与之对应,无论是js还是java或者其他的技术都可以运用. 既然是分层那么这些层都有哪些职责呢? View层(界面层): 为用户展示 ...

  10. 【真的是随笔】如何利用htaccess把网站流量引到一个页面上(站点维护页效果)

    咕咕咕,好久没来博客园转悠了,最近(这个最近好长啊)一直没时间写博(事实上也不知道写点什么有逼格的东西),所以一直都在潜水,,,(此处省略n字) 好了切入正题,关于如何把网站的所有流量引向一个页面的方 ...