JSON parsing to Java – Android application

Using manual parsing you can implement it like this:

            JSONArray  pages     =  new JSONArray(jsonString);
            for (int i = 0; i < pages.length(); ++i) {
                JSONObject rec = pages.getJSONObject(i);
                JSONObject jsonPage =rec.getJSONObject("page");
                String address = jsonPage.getString("url");
                String name = jsonPage.getString("name");
                String status =  jsonPage.getString("status");
}

in your case note that your outer elemnt data is type of JSONObject and then you have a JSONArray

mine json file:

[{"page":{"created_at":"2011-07-04T12:01:00Z","id":1,"name":"Unknown Page","ping_at":"2011-07-04T12:06:00Z","status":"up","updated_at":"2011-07-04T12:01:00Z","url":"http://www.iana.org/domains/example/","user_id":2}},{"page":{"created_at":"2011-07-04T12:01:03Z","id":3,"name":"Down Page","ping_at":"2011-07-04T12:06:03Z","status":"up","updated_at":"2011-07-04T12:01:03Z","url":"http://www.iana.org/domains/example/","user_id":2}}] 

note that mine starts from [, which means an array, but yours from { and then you have [ array inside. If you run it with a debugger, you can see exactly what´s inside your json objects.

There are also better approaches like:

  1. Jackson
  2. Jackson-JR (light-weight Jackson)
  3. GSON

All of them can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

Leave a Comment