json对象处理
1、## 标题递归遍历所有key的类型
import org.json.JSONArray;
import org.json.JSONObject;
import com.jayway.jsonpath.*;
import java.util.Iterator;
import java.util.List;
public static void JosnType(JSONObject jsonObject) {
for (Iterator keys = jsonObject.keys(); keys.hasNext(); ) {
String key = (String) keys.next();
// System.out.println(“key:” + key);
String type = jsonObject.get(key).getClass().getName();
if (type == “org.json.JSONArray”) {
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
JosnType(jsonArray.getJSONObject(i));
}
} else if (type == "org.json.JSONObject") {
JosnType(jsonObject.getJSONObject(key));
} else {
System.out.println(key + ":" + type);
}
}
}
//jsonpath获取key的值
Object names = JsonPath.read(str,"$.student");
String arr=names.toString();
if(arr.startsWith("["))
{
System.out.println("数组");
}