Json   发布时间:2022-04-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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,"value":"ab2"},{"exp":"AB=ab3","count":210,"value":"ab3"},{"exp":"TI=aa1","count":10,"name":"TI","value":"aa1"},{"exp":"TI=aa2","count":15,"value":"aa2"},{"exp":"TI=aa3","count":20,"value":"aa3"}]}

大佬总结

以上是大佬教程为你收集整理的jsonobject 遍历 org.json.JSONObject全部内容,希望文章能够帮你解决jsonobject 遍历 org.json.JSONObject所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。