-
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"}]}
-
相关阅读:
python函数名和左括号之间不能有空格
linux版本选择
filter_map
awk统计总结
Spring Boot + Redis 实现各种操作
手机号正则校验
判断windows系统
Redis分布式锁
shell 脚本快速部署 SpringBoot 项目
Java主流的Http请求--原生的HttpURLConnection
-
原文地址:https://www.cnblogs.com/ldq2016/p/5780881.html
Copyright © 2020-2023
润新知