jsonobject jsonarray

简介:   jsonobject jsonarray       public static void main(String[] args) { String str = "{'TI':[{'value':'aa1','count':10},{'value':...

 

jsonobject jsonarray

 

 

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

 

目录
相关文章
|
XML 存储 JSON
JSON、JSONObject 与 JSONArray 详细介绍及其应用方式
JSON、JSONObject 与 JSONArray 详细介绍及其应用方式
2165 1
JSON、JSONObject 与 JSONArray 详细介绍及其应用方式
|
4月前
|
JSON Java 数据格式
如何用String字符串生成JSONObject和JSONArray数据
如何用String字符串生成JSONObject和JSONArray数据
1646 1
|
3月前
|
JSON Java 数据格式
使用JSONObject解析与生成JSON数据
使用JSONObject解析与生成JSON数据
|
JSON 数据格式 fastjson
JsonNode、JsonObject常用方法
JsonNode、JsonObject常用方法 最近项目中要用json,闲暇时间,对json进行下总结。 1.JsonNode 项目中用到的jar包 import com.
7481 0
|
JSON 数据格式
JsonArray和JsonObject遍历方法
一:遍历JsonArray // 一个未转化的字符串 String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'d...
7099 0