jsonobject 遍历 org.json.JSONObject
2016-03-22 09:44
423 查看
[html]
view plain
copy
print?
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
[java]
view plain
copy
print?
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();
}
}
[java]
view plain
copy
print?
{"groups":[{"exp":"AB=ab","count":110,"name":"AB","value":"ab"},{"exp":"AB=ab2","count":115,"name":"AB","value":"ab2"},{"exp":"AB=ab3","count":210,"name":"AB","value":"ab3"},{"exp":"TI=aa1","count":10,"name":"TI","value":"aa1"},{"exp":"TI=aa2","count":15,"name":"TI","value":"aa2"},{"exp":"TI=aa3","count":20,"name":"TI","value":"aa3"}]}
[html]
view plain
copy
print?
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 plain
copy
print?
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','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(); } }
[java]
view plain
copy
print?
{"groups":[{"exp":"AB=ab","count":110,"name":"AB","value":"ab"},{"exp":"AB=ab2","count":115,"name":"AB","value":"ab2"},{"exp":"AB=ab3","count":210,"name":"AB","value":"ab3"},{"exp":"TI=aa1","count":10,"name":"TI","value":"aa1"},{"exp":"TI=aa2","count":15,"name":"TI","value":"aa2"},{"exp":"TI=aa3","count":20,"name":"TI","value":"aa3"}]}
{"groups":[{"exp":"AB=ab","count":110,"name":"AB","value":"ab"},{"exp":"AB=ab2","count":115,"name":"AB","value":"ab2"},{"exp":"AB=ab3","count":210,"name":"AB","value":"ab3"},{"exp":"TI=aa1","count":10,"name":"TI","value":"aa1"},{"exp":"TI=aa2","count":15,"name":"TI","value":"aa2"},{"exp":"TI=aa3","count":20,"name":"TI","value":"aa3"}]}
相关文章推荐
- Objective-C对象与Core Foundation对象
- Newtonsoft.Json之JArray, JObject, JProperty,JValue
- object-c第三方FMDB对sqlite使用
- object-c第三方FMDB对sqlite使用
- C++ 进阶——object slicing 与虚函数与dynamic_cast<>
- java综合之 链表 接口 Object对象
- Objective-C 在View中跳转页面
- Objective-C 继承新的认识以及作用
- objective-C CollectionView 加深(添加注册头部View)
- Objective-C探究alloc方法的实现
- Objective-C语法之代码块(block)的使用
- Objective-C语法之代码块(block)的使用
- Object-C高级编程读书笔记(4)——__block说明符
- java学习笔记6--类的继承、Object类
- iOS下Objective-C的图片轮播器分析与源代码分享
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
- 控制gameObject随机旋转的功能代码
- UI - NSObject 映射关系图
- 值得一说的NullObject模式
- iOS安全攻防(二十三):Objective-C代码混淆