Question

Here i want to fetch the data from the json, but i am getting only first two objects value (25, 44) but the ids are 50,60 . I don't know whats wrong with this code.

Below is my response from the server:

{
"product": {
    "25": {
        "training": "First Name",
        "taken": null,
        "date": "1386737285",
        "body":"http://abc.xyz.in/video1.mp4",
        "image": "http://abc.xyz.in/video1.jpg"
    },
    "44": {
        "training": "Second Name",
        "taken": null,
        "date": "1389951618",
        "body":"http://abc.xyz.in/video2.mp4",
        "image":"http://abc.xyz.in/video2.jpg"
    },
    "50": {
        "training": "Third Name",
        "taken": null,
        "date": "1389971004",
        "body":"http://abc.xyz.in/video3.mp4",
        "image": "http://abc.xyz.in/video3.jpg"
    },
    "60": {
        "training": "Fourth Name",
        "taken": null,
        "date": "1390003200",
        "body": "http://abc.xyz.in/video4.mp4",
        "image": "http://abc.xyz.in/video4.jpg"
    }
  }
}

Here is the code for fetching data from json:

 public String[] getDataFromResponse(String jsonProfileResponse,String secondParam,
        String attributeName ) {

    String[] attributeValue = null;
    try {
        json = new JSONTokener(jsonProfileResponse).nextValue();
        if (json instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) json;
            JSONObject jObj = jsonObject.getJSONObject(secondParam);
            System.out.println(jObj);
            Iterator<?> keys = jObj.keys();
            List<String> listitems = new ArrayList<String>();
            List<String> nids = new ArrayList<String>();
            while (keys.hasNext()) {
                nids.add(String.valueOf(keys.next()));
                JSONObject jsonObj = jObj.getJSONObject(String.valueOf(keys
                        .next()));
                System.out.println(jsonObj);
                listitems.add(jsonObj.getString(attributeName));
            }
            attributeValue = listitems.toArray(new String[0]);
                trainingId = nids.toArray(new String[0]);
        }

    } catch (JSONException ex) {
        ex.printStackTrace();
    }
    return attributeValue;

}

Thanks for the considering...

Was it helpful?

Solution

Inside the hasNext you call twice keys.next()

So, instead of

  nids.add(String.valueOf(keys.next()));
  JSONObject jsonObj = jObj.getJSONObject(String.valueOf(keys.next()));

you have to do

  String currentKey = String.valueOf(keys.next());
  nids.add(currentKey);
  JSONObject jsonObj = jObj.getJSONObject(currentKey);

OTHER TIPS

String key="";
while (keys.hasNext()) {
   key= keys.next()
   JSONObject jsonObj = jObj.getJSONObject(String.valueOf(key));
   nids.add(key));
   System.out.println(jsonObj);
   listitems.add(jsonObj.getString(attributeName));
}

use of key.next() twice is problem

because in JSONObject, the order of the keys is undefined.

@see: http://developer.android.com/reference/org/json/JSONObject.html#keys%28%29

try to sort your data on server, then response it in JSONArray

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top