json遍历 分别使用【原】

时间:2022-06-14 16:20:49

json遍历

一 使用org.json.JSONObject遍历

之后的所有遍历都参考了:http://blog.csdn.net/u010648555/article/details/49815387 的设计思路.

package test.json;

import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; /**
* 该类引用自QQ号为742981086同学的链接http://blog.csdn.net/u010648555/article/details/49815387
* 思路很清晰,值得推荐.
* traverseJson(...)被我修改过注释.
* traverse1()保持不变
* traverse2()被我改过样例
*
* @author 359920692
*
*/
public class TraverseJson { /**
* 遍历json格式数据
*
* @param jsonObjArg
* @return
* @throws JSONException
*/
public static Object traverseJson(Object jsonObjArg) throws JSONException {
if (jsonObjArg == null) {
return null;
}
if (jsonObjArg instanceof JSONObject) {// 如果是JSONObject实例 (其实JSONObject内存就是以map格式存储的)
// 创建json对象
JSONObject retJsonObj = new JSONObject();
// 将json转换为JsonObject对象
JSONObject jsonObj = (JSONObject) jsonObjArg;
// 迭代 map所有的keys
Iterator it = jsonObj.keys();
while (it.hasNext()) {
// 获取map的key
String key = (String) it.next();
// 得到value的值
Object value = jsonObj.get(key);
// 递归遍历
retJsonObj.put(key, traverseJson(value));
}
return retJsonObj;
} else if (jsonObjArg instanceof JSONArray) {// 如果是JSONArray 实例
JSONArray retJsonArray = new JSONArray();
JSONArray jsonArray = (JSONArray) jsonObjArg;
// 获取Array 的长度
int length = jsonArray.length();
for (int i = 0; i < length; i++) {
retJsonArray.put(traverseJson(jsonArray.get(i)));
}
return retJsonArray;
} else {
// 除了JSONObj,JSONArray就只有最原始的String类型了,此时可以对String进行
// "加/解密,变换等操作",如return
// jsonObjArg+"^_^",可以让每个值都微笑起来
return jsonObjArg;
}
} /* 生成的JSON数据1
{
"QQ": [
"742981086@qq.com",
"742981086"
],
"name": "aflyun",
"age": 22,
"hobby": [
"编程",
"看书",
"徒步",
"爬山",
"游泳"
],
"adderss": {
"省份": "广东",
"市": "惠州",
"国籍": "中国"
}
}
*/
public static void traverse1() throws JSONException {
// 创建 一个JsonObjec对象
JSONObject retJsonObj = new JSONObject();
// 姓名
retJsonObj.put("name", "aflyun");
// 年龄
retJsonObj.put("age", 22);
// 联系方式
JSONArray arryQQ = new JSONArray();
arryQQ.put("742981086@qq.com").put("742981086");
retJsonObj.put("QQ", arryQQ);
// 地址 map
JSONObject jsonAdress = new JSONObject();
jsonAdress.put("country", "中国").put("省份", "广东").put("市", "惠州");
retJsonObj.put("adderss", jsonAdress);
// 生成数组array
JSONArray jArray = new JSONArray();
jArray.put("编程").put("看书").put("徒步").put("爬山").put("游泳");
retJsonObj.put("hobby", jArray);
System.out.println("1_________________________________________________________");
System.out.println(retJsonObj);
System.out.println(traverseJson(retJsonObj));
System.out.println("1_________________________________________________________\n");
} /*
生成数组类型的json格式数据
[
{
"Province": "ZheJiang"
},
[
{
"Person": "bobo"
},
{
"Fruit": [
"Apple",
"Banana",
"Pear"
]
}
]
]
*/
public static void traverse2() throws JSONException {
JSONArray retJArray = new JSONArray();
JSONObject aJson = new JSONObject();
aJson.put("Province", "ZheJiang");
retJArray.put(aJson);
JSONArray jArray1 = new JSONArray();
JSONObject personObj = new JSONObject();
personObj.put("Person", "bobo");
JSONArray fruitArray = new JSONArray();
fruitArray.put("Banana").put("Apple").put("Pear");
JSONObject fruitObj = new JSONObject();
fruitObj.put("Fruit", fruitArray);
jArray1.put(personObj).put(fruitObj);
// 将组装好的数据放入要返回的json数组中
retJArray.put(jArray1); System.out.println("2_________________________________________________________");
System.out.println(retJArray);
System.out.println(traverseJson(retJArray));
System.out.println("2_________________________________________________________\n");
} public static void main(String[] args) throws JSONException {
traverse1();
traverse2();
} }

二. 使用 com.alibaba.fastjson.JSON  fastjson遍历

fastJson顺序遍历JSON字段(转)

FastJson的基本使用

FastJson一些常见方法(API):处理JSON变得好简单