简体   繁体   English

Android未解析JSON数组

[英]Android not parsing JSON Array

I am currently trying to parse a JSON Array in my code but the problem is it never enters the try block. 我目前正在尝试解析代码中的JSON数组,但问题是它从未进入try块。

Here is my code: 这是我的代码:

protected Void doInBackground(Void... arg0) {

        JsonParser jsonParser = new JsonParser();

        String json =  jsonParser.getJSONFromUrl("http://hills.ccsf.edu/~jpark41/ViB/sample.json");


        if(json != null) {

 Log.i("JSON: ", "NOT NULL"); // This log executes!!

            try
            {
                Log.i("we: ", " out here"); // This does not!!

                JSONObject jObj = new JSONObject(json);
                JSONArray jsar = new JSONArray(json);

                Iterator<String> iter = jObj.keys();
                while (iter.hasNext()) {
                    String key = iter.next();
                    try {
                        JSONObject value = (JSONObject)jObj.get(key);
                        id = value.getString("id");
                        thumb = value.getString("thumb");
                        media = value.getString("media");
                        title = value.getString("title");
                    } catch (JSONException e) {
                        // Something went wrong!
                    }
                    now_playing = id;
                    earned = title;

                }

            } catch (JSONException e) {
                Log.i("we NOT: ", " out here");
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return null;
    }

"we out here" is never logged, only "we NOT out here". 永远不会记录“我们不在这里”,只有“我们不在这里”。 Why is the try block never executing? 为什么try块永远不会执行? I don't have much experience parsing JSON with android so I'm sure I'm just missing something. 我没有用Android解析JSON的丰富经验,所以我确定我只是缺少一些东西。 Thanks for any help. 谢谢你的帮助。

When i commented out the JSONObject jObj = new JSONObject(json); 当我注释掉JSONObject jObj = new JSONObject(json); and swapped out the iterator for a basic for loop it all worked. 并将迭代器换成一个基本的for循环,所有这些都起作用了。 Thanks for everyones help. 谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM