1

我在尝试使用 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

我犯了什么愚蠢的错误?

4

2 回答 2

2

我能够以这种方式获得privacyStatus,但是我似乎在他们的文档中看不到任何内容,其中显示了使用链式get语句的任何示例,例如您所拥有的。

((JSONObject)((JSONObject)((JSONArray) jsonObject.get("items")).get(0)).get("status")).get("privacyStatus")

编辑:我在我拥有的一些 android 代码中发现了这个小片段它将与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);
    }
}
于 2013-06-02T01:39:52.050 回答
2

我想以干净的方式解决它是一个库限制。我找到了最小的库: 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();
于 2013-06-02T04:31:43.207 回答