`
knight_black_bob
  • 浏览: 827227 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

json 遍历 jsonobject jsonarray

    博客分类:
  • json
阅读更多

 

 

 

 

 

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();  
        }  
    }  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics