加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

jsonobject 遍历取值

发布时间:2020-12-16 19:14:35 所属栏目:百科 来源:网络整理
导读:例子如下: import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;[java] view plaincopyprint?public static void main(String[]



例子如下:

import org.json.JSONArray;  
import org.json.JSONException;  
import org.json.JSONObject;  

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
[java] view plaincopyprint?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();  
        }  
    }  
      

public static void main(String[] args) {
		String str = "{'TI':[{'value':'aa1'," +
				"'AB':[{'value':'ab','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();
		}
	}



结果:

[java] view plaincopyprint?{"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"}]}



原文链接:

http://blog.csdn.net/wwwyuanliang10000/article/details/7648250

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读