我在尝试使用json-simple解析字符串时遇到问题,这是示例字符串:
{
"items": [
{
"id": "uy0nALQEAM4",
"kind": "youtube#video",
"etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/x3SYRGDdvDsN5QOd7AYVzGOJQlM\"",
"status": {
"uploadStatus": "processed",
"privacyStatus": "public",
"license": "youtube",
"embeddable": true,
"publicStatsViewable": true
}
}
]
}
这是我的代码:
JSONParser parser = new JSONParser();
Object obj = parser.parse(result);
JSONObject jsonObject = (JSONObject) obj;
System.out.println("privacyStatus: "
+ (String) jsonObject.get("items[0].status.privacyStatus")
+ "\nembeddable: "
+ (String) jsonObject.get("items[0].status.embeddable")
+ "\npublicStatsViewable: "
+ (String) jsonObject.get("items[0].status.publicStatsViewable"));
输出结果为:
privacyStatus: null
embeddable: null
publicStatsViewable: null
我犯了什么愚蠢的错误?
答案 0 :(得分:2)
我能够以这种方式获得privacyStatus,但是我似乎无法在他们的文档中看到任何显示使用链式get语句的示例,例如您拥有的语句。
((JSONObject)((JSONObject)((JSONArray) jsonObject.get("items")).get(0)).get("status")).get("privacyStatus")
编辑:我在一些安卓代码中发现了这个小片段它可以与http://json.org/java/库一起工作(这与android JSON库非常相似)
public static void main(String[] args)
{
// this is the same JSON string in the OP
String jsonString = "{ \"items\": [ { \"id\": \"uy0nALQEAM4\", \"kind\": \"youtube#video\", \"etag\": \"\\\"g-RLCMLrfPIk8n3AxYYPPliWWoo/x3SYRGDdvDsN5QOd7AYVzGOJQlM\\\"\", \"status\": { \"uploadStatus\":\"processed\", \"privacyStatus\": \"public\", \"license\": \"youtube\", \"embeddable\": true, \"publicStatsViewable\": true } } ]}";
JSONObject object = new JSONObject(jsonString);
try {
String myValue = (String)getJSONValue("items[0].status.privacyStatus", object);
System.out.println(myValue);
} catch (JSONException ex) {
Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static Object getJSONValue(String exp, JSONObject obj) throws JSONException {
try {
String [] expressions = exp.split("[\\.|\\[|\\]]");
Object currentObject = obj;
for(int i=0; i < expressions.length; i++) {
if(!expressions[i].trim().equals("")) {
System.out.println(expressions[i] + " " + currentObject);
if(currentObject instanceof JSONObject) {
Method method = currentObject.getClass().getDeclaredMethod("get", String.class);
currentObject = method.invoke(currentObject, expressions[i]);
} else if(currentObject instanceof JSONArray) {
Method method = currentObject.getClass().getDeclaredMethod("get", Integer.TYPE);
currentObject = method.invoke(currentObject, Integer.valueOf(expressions[i]));
} else {
throw new JSONException("Couldnt access property " + expressions[i] + " from " + currentObject.getClass().getName());
}
}
}
return currentObject;
} catch (NoSuchMethodException ex) {
throw new JSONException(ex);
} catch (IllegalAccessException ex) {
throw new JSONException(ex);
} catch (IllegalArgumentException ex) {
throw new JSONException(ex);
} catch (InvocationTargetException ex) {
throw new JSONException(ex);
}
}
答案 1 :(得分:1)
我想这是一个以简洁方式解决它的库限制。我找到了最小的库: https://github.com/ralfstx/minimal-json
哪个非常干净整洁。然后做了以下做我想做的事情:
JsonObject jsonObject = JsonObject.readFrom(result.toString())
.get("items").asArray().get(0).asObject().get("status").asObject();
然后我可以做:
boolean isPublic = jsonObject.get("privacyStatus").asString().equals("public");
boolean isEmbbedable = jsonObject.get("embeddable").asBoolean();