简体   繁体   中英

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.

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? I don't have much experience parsing JSON with android so I'm sure I'm just missing something. Thanks for any help.

When i commented out the JSONObject jObj = new JSONObject(json); and swapped out the iterator for a basic for loop it all worked. Thanks for everyones help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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