0

I'm having some issues consuming a JSON API in Android Studio I've been watching and reading about a lot of examples, but still can't figure out how to extract the data I need...

After some hours, I have finally gotten to the point were I figured to extract:

JSONObject subObj = jsonObject.getJSONObject("response");

With the following contents:

{
    "hereNow": {
    "count": 1,
    "items": [
      {
        "id": "xxxxxxxxxx",
        "createdAt": xxxxxxxxxx,
        "type": "checkin",
        "timeZoneOffset": 60,
        "user": {
          "id": "xxxxxxx",
          "firstName": "xxxxxxxx",
          "lastName": "xxxxxxxxxx",
          "gender": "male",
          "relationship": "friend",
          "photo": {
            "prefix": "xxxxxxxxx",
            "suffix": "xxxxxxxxxx"
          }
        },
        "likes": {
          "count": 0,
          "groups": [

      ]
    },
    "like": false
      }
    ]
  }
}

The problem now is: How to extract the user object? I've been trying a lot of things with

JSONObject

and

JSONArray

But I keep on getting this:

Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference

Can someone please help me? Please don't redirect me to another topic, because in general I understand JSON, just not in this particular case...

Thanks in advance.

arnoutvh
  • 141
  • 1
  • 3
  • 13
  • try the example and read the [explanation here](http://stackoverflow.com/questions/5015844/parsing-json-object-in-java/40828056#40828056) and update your question if you have doubts after reading – Pavneet_Singh Dec 10 '16 at 13:44

1 Answers1

0

This should work for you

    try{
        JSONArray array = subObj.getJSONObject("hereNow").getJSONArray("items");
        for(int index=0; index<array.length();index++){
            JSONObject itemsObj = array.getJSONObject(index);
            JSONObject userObj = itemsObj.getJSONObject("user");
            Log.d("Test", "Relationship "+userObj.get("relationship"));
        }   
    } catch (JSONException e) {
        e.printStackTrace();
    }