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. listview复用机制研究

    Listview在第一次的时候会先把屏幕上绘制的item都new出来,为了讲解方便我把new出来的item都用红色背景,复用的则用绿色背景. 可以看到这个list种有三种item.在第一次展示的时候, ...

  2. Python学习02 列表 List

    Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...

  3. <转>关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系

    转自:http://www.cnblogs.com/cywin888/p/3263027.html 刚接触iOS开发的人难免会对苹果的各种证书.配置文件等不甚了解,可能你按照网上的教程一步一步的成功申 ...

  4. SwipeBackActivity 的使用

    1.SwipeBackLayout 项目地址:https://github.com/ikew0ng/SwipeBackLayout 2.用法 android studio  compile 'me.i ...

  5. UITextView: 响应键盘的 return 事件(收回键盘)

    UITextView: 响应键盘的 return 事件(收回键盘) 此篇文章将要介绍UITextView: 响应键盘的 return 事件(收回键盘)的相关介绍,具体实例请看下文 UITextView ...

  6. Windows 编程中的字符串(2)

    (1)windows写日志系统 void writeDebugEventLog(TCHAR* pszMessage, WORD wType) { //#ifdef _DEBUG HANDLE hEve ...

  7. LB 简单比较 – F5、NetScaler、LVS、Nginx、Haproxy

    LB 简单比较 – F5.NetScaler.LVS.Nginx.Haproxy 负载均衡技术是构建大型网站必不可少的架构策略之一.它的目的是,把用户的请求分发到多台后端的设备上,用以均衡服务器的负载 ...

  8. App.Config 和 WebConfig 特殊字符的转义码对应关系

    Web.Config默认编码格式为UTF-8,对于XML文件,要用到实体转义码来替换.对应关系如下: 字符 转义码 & 符号 & & 单引号 ' &apos; 双引号 ...

  9. 【转发】NPAPI开发详解,Windows版

    NPAPI开发详解,Windows版 9 jiaofeng601, +479 9人支持,来自Meteor.猪爪.hanyuxinting更多 .是非黑白 .Yuan Xulei.hyolin.Andy ...

  10. Bootstrap弹出框(modal)垂直居中

    最近在做一个eit项目,由于此项目里面一些框架要遵循nttdata的一些规则,故用到了Bootstrap这个东东,第一次碰到这个东东,有很大抵触,觉得不好,但用起来我觉得和别的弹出框真没什么两样.废话 ...