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捐助),没钱捧个人场,谢谢各位。



谢谢您的赞助,我会做的更好!
本文展示了如何使用Java处理JSON数据,包括解析JSON字符串为对象,操作JSON数组和对象,以及创建新的JSON结构。重点在于理解JSON数据格式及其实现方式。
339

被折叠的 条评论
为什么被折叠?



