首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JSON-Simple的JSON解析不起作用

使用JSON-Simple的JSON解析不起作用
EN

Stack Overflow用户
提问于 2013-06-02 09:20:55
回答 2查看 2.4K关注 0票数 1

我在尝试使用json-simple解析字符串时遇到问题,这是示例字符串:

代码语言:javascript
复制
{
 "items": [
  {
   "id": "uy0nALQEAM4",
   "kind": "youtube#video",
   "etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/x3SYRGDdvDsN5QOd7AYVzGOJQlM\"",
   "status": {
    "uploadStatus": "processed",
    "privacyStatus": "public",
    "license": "youtube",
    "embeddable": true,
    "publicStatsViewable": true
   }
  }
 ]
}

这是我的代码:

代码语言:javascript
复制
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"));

输出为:

代码语言:javascript
复制
privacyStatus: null
embeddable: null
publicStatsViewable: null

我犯了什么愚蠢的错误?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-02 12:31:43

我猜这是一个库的限制,无法以一种干净的方式解决它。我找到了最小的库:https://github.com/ralfstx/minimal-json

这是非常好和干净的。然后做了以下事情来做我想做的事情:

代码语言:javascript
复制
JsonObject jsonObject = JsonObject.readFrom(result.toString())
          .get("items").asArray().get(0).asObject().get("status").asObject();

然后我可以这样做:

代码语言:javascript
复制
boolean isPublic = jsonObject.get("privacyStatus").asString().equals("public");
boolean isEmbbedable = jsonObject.get("embeddable").asBoolean();
票数 2
EN

Stack Overflow用户

发布于 2013-06-02 09:39:52

我能够以这种方式获得privacyStatus,但是我似乎在他们的文档中看不到任何使用链式get语句的示例。

代码语言:javascript
复制
((JSONObject)((JSONObject)((JSONArray) jsonObject.get("items")).get(0)).get("status")).get("privacyStatus")

编辑:我在一些安卓代码中发现了这个小片段,它将与http://json.org/java/库一起工作(这与android JSON库非常相似,如果不是相同的话)

代码语言:javascript
复制
   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);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16878900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档