Search code examples
javaandroidjsongetstring

JSONArray not returning correctly


I have this JSON data and am trying to process in in Java.

{  
   "status":"ok",
   "data":{  
      "user_count":3,
      "limit":20,
      "page_number":1,
      "users":[  
         {  
        "id":330,
            "name":"jeff",
            "email":"xxx@hotmail.com
         },
            "id":335,
            "name":"joe",
            "email":"xxx@hotmail.com
         }
      ]
    },
}

I am able to grab the status but will not grab all the users, which is strange as I have another JSON sample that does work.

Here my is my code:

                    results = response.getString("status");
                    if (results.equals("ok")) {
                        JSONArray records = response.getJSONArray("data");
                        for (int i = 0; i < records.length(); i++) {
                            JSONObject obj = records.getJSONObject(i);
                            JSONArray users = obj.getJSONArray("users");
                            for (int j = 0; j < users.length(); j++) {
                                JSONObject v_obj = users.getJSONObject(j);
                                Log.d("Dracula", v_obj.getString("id"));
                                Log.d("Dracula", v_obj.getString("name"));
                                Log.d("Dracula", v_obj.getString("email"));
                            }
                        }
                    }

Is the JSON malformed, or something? Any help is appreciated.


Solution

  • Yes it is heavily malformed :) Here's the corrected one.

    {
      "status": "ok",
      "data": {
        "user_count": 3,
        "limit": 20,
        "page_number": 1,
        "users": [
          {
            "id": 330,
            "name": "jeff",
            "email": "xxx@hotmail.com"
          },
          {
            "id": 335,
            "name": "joe",
            "email": "xxx@hotmail.com"
          }
        ]
      }
    }
    

    Errors in your JSON:

    1. Double quotes completion missing after emails
    2. Extra , after details:{} object
    3. Second user object is missing a opening {

    Here's a good tool to check your JSON files. Try copy pasting your JSON in it, and try mine. You will see the difference

    UPDATE Moving on to potential Java Issues:

    1. Your data is not a array but an object, and yet in Java code you are parsing it as one JSONArray records = response.getJSONArray("data");

    Also, there is a chance that problem is actually in the web services. Sometimes web-services change the datatype from array [] to object {} when only one result is found. Which is why your code which parses data as a array fails for this object. You should either have your dataservice fixed to return it as array of one object like so data:[{..}] or ad a check in java to see if data is an array or Object.

    If you do confirm that data always comes in as a Object (which makes sense looking at the context of your JSON), then switch the code to not iterate on data but to iterate on data.users like so:

     JSONObject records = response.getJSONObject("data");
     JSONArray users = records.getJSONArray("users");
    
      for (int j = 0; j < users.length(); j++) {
              JSONObject v_obj = users.getJSONObject(j);
               Log.d("Dracula", v_obj.getString("id"));
               Log.d("Dracula", v_obj.getString("name"));
               Log.d("Dracula", v_obj.getString("email"));
      }