0

I have this JSON string:

{
  "query": {
    "pages": {
      "53113": {
        "pageid": 53113,
        "ns": 0,
        "title": "Charing Cross",
        "coordinates": [
          {
            "lat": 51.5073,
            "lon": -0.12755,
            "primary": "",
            "globe": "earth"
          }
        ]
      },
      "33109245": {
        "pageid": 33109245,
        "ns": 0,
        "title": "Equestrian statue of Charles I, Charing Cross",
        "coordinates": [
          {
            "lat": 51.5073,
            "lon": -0.12768,
            "primary": "",
            "globe": "earth"
          }
        ]
      },
      "4347521": {
        "pageid": 4347521,
        "ns": 0,
        "title": "Greater London Built-up Area",
        "coordinates": [
          {
            "lat": 51.5073,
            "lon": -0.1277,
            "primary": "",
            "globe": "earth"
          }
        ]
      },
      "17867": {
        "pageid": 17867,
        "ns": 0,
        "title": "London",
        "coordinates": [
          {
            "lat": 51.5072,
            "lon": -0.1275,
            "primary": "",
            "globe": "earth"
          }
        ]
      }
    }
  }
}

Ho do I parse it? I wrote this code but I wasn't able to iterate through the json string. I need the "title" objects and the "coordinates" array of "lat" and "lon"...

I solved it in the end. Thank you all solved it like this:

            try {
            // Parsing JSON String or URL
            JSONObject jsonObj = new JSONObject(jsonurl);


            // grabbing objects 
            JSONObject obj_query = jsonObj.getJSONObject(TAG_QUERY);
            JSONObject obj_pages = obj_query.getJSONObject(TAG_PAGES); 
            JSONArray arr_id = obj_pages.names();

            for (int i = 0 ; i < arr_id.length() ; i ++)
            {
                JSONObject obj_id = obj_pages.getJSONObject(arr_id.get(i).toString());
                // Log.i(LOGTAG, "obj_id: " + obj_id.toString());

                String tag_pageid = obj_id.getString(TAG_PAGEID); 
                // String tag_ns = obj_id.getString(TAG_NS);
                String tag_title = obj_id.getString(TAG_TITLE); 
                Log.i(LOGTAG, "page id: " +  tag_pageid); 
                // Log.i(LOGTAG, tag_ns); 
                Log.i(LOGTAG, "Title: " + tag_title);
                // using JSONArray to grab the TAG_COORDINATES 

                JSONArray arr_coord = obj_id.getJSONArray(TAG_COORDINATES);  
                // lets loop through the JSONArray and get all the items 
                for (int j = 0; j < arr_coord.length(); j++) { 
                    // printing the values to the logcat 
                    Log.i(LOGTAG, "lat:" + arr_coord.getJSONObject(j).getString(TAG_LAT).toString()); 
                    Log.i(LOGTAG, "lon: " + arr_coord.getJSONObject(j).getString(TAG_LON).toString()); 
                } 

            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
MPelletier
  • 16,256
  • 15
  • 86
  • 137
Guy West
  • 424
  • 4
  • 17
  • I don't get what it's your problem, I just run your code and all work ok... – GhostDerfel Jan 07 '14 at 17:04
  • pages.keys() ... Asked so many times here – Selvin Jan 07 '14 at 17:06
  • this ```JSONObject tag_id = tag_pages.getJSONObject(TAG_PAGES);``` looks like it should be changed to something more like this ```JSONObject tag_id = tag_pages.getJSONObject(TAG_ID);``` I suspect that line of code is not returning what you expect from the json. You want the Id out of the ```tag_pages``` object right? – Anderman Jan 07 '14 at 17:38
  • Thank you all I solved it in the end. – Guy West Jan 08 '14 at 14:35

1 Answers1

1

My droidQuery library has some methods that can greatly simplify this task:

$.getJSON(jsonurl, null, new Function() {
    @Override
    public void invoke($ d, Object… args) {
        JSONObject json = (JSONObject) args[0];
        JSONObject query = json.getJSONObject(TAG_QUERY);
        JSONObject pages = json.getJSONObject(TAG_PAGES);
        Map<String, ?> map = $.map(pages);
        for (Entry<String, ?> entry : map.entrySet()) {
            String key = entry.getKey();//this is the number, such as 53113
            JSONObject value = (JSONObject) entry.value();
            Map<String, ?> data = $.map(value);//contains all the fields you need
            String title = (String) data.get("title");//<--- THIS IS THE TITLE FIELD
            JSONArray array = (JSONArray) data.get("coordinates");
            Object[] data = $.array(array);
            for (Object o : data) {
                //loops through each coordinates object
                JSONObject coordinates = (JSONObject) o;//<--- THIS IS THE FIRST COORDINATE OBJECT
                //now handle this coordinates data:
                Map<String, ?> coords = $.map(coordinates);
                double lat = (double) coords.get("lat");
                double lon = (double) coords.get("lon");
                //etc...
            }
        }
    }
});
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Phil
  • 35,852
  • 23
  • 123
  • 164
  • Wow. @Phil this Android port of jQuery sounds really cool, but at the moment I want to focus on standard Android practices. in the future I will use it in my projects. I posted it to my google plus. https://plus.google.com/+GuyWestGuy/posts/RdjUMZpNviC – Guy West Jan 08 '14 at 10:54