0

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...

Mohan
3118 silver badges15 bronze badges
asked Jan 28, 2014 at 8:08
3
  • Have you considered using Jackson or GSON for automatically creating java objects from json? It would cost a bit of performance (just a bit, really), but you will do that all in one or thwo lines of code. Commented Jan 28, 2014 at 8:17
  • Tell me how to do that?? @user1685095 Commented Jan 28, 2014 at 8:19
  • Well, the tutorial are on their site. But here is one mkyong.com/java/how-to-convert-java-object-to-from-json-jackson Basically you would need to create java object with corresponding fields for every field in json. And then you would get such object from json for free, without need of parsing it by hand. Commented Jan 28, 2014 at 9:06

3 Answers 3

3

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);
answered Jan 28, 2014 at 8:15
Sign up to request clarification or add additional context in comments.

4 Comments

How can i store the keys @alessio
where do you need to store the key? your code is ok except from this two lines. I should use GSON instead of doing it manually BTW, it's very simple and you can avoid this kind of problems. Check here code.google.com/p/google-gson
@Alession Please give me a small example to get the values by above response by using GSON.
the official documentation is the fastest way to learn how to do it, trust me, in 10 minutes you'll have your code up and running with gson ;) sites.google.com/site/gson/gson-user-guide. In a minute: you define a class that represent your json data, you call the "fromJson(String json,Class yourClass)" method, and gson will create an instance of yourClass parsing the json
1
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

answered Jan 28, 2014 at 8:15

Comments

0

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

answered Jan 28, 2014 at 8:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.