1

I have JSONArray and when I decode JSON to HashMap at that time HashMap take last value of JSONArray.

here my code:

QjArray = new JSONArray(Questionresult);
JSONObject json_data  = new JSONObject();
for (int i = 0; i<QjArray.length(); i++) {
    objJMap = new HashMap<String, String>(); 
    json_data = QjArray.getJSONObject(i);

    jQuestionName =json_data.getString("QuestionName");
    objJMap.put("QuestionName",jQuestionName);

    jQuestiontypeid = json_data.getInt("Questiontypeid");
    String Qid = jQuestiontypeid.toString();
    objJMap.put("Questiontypeid", Qid);

    jAnswertypeid = json_data.getInt("Answertypeid");
    String Aid = jAnswertypeid.toString();
    objJMap.put("Answertypeid", Aid);
}

My JSONArray:

This is question list[{"QuestionID":"1","QuestionName":"when you come","Questiontypeid":"1","Answertypeid":"1"},{"QuestionID":"2","QuestionName":"about your words","Questiontypeid":"1","Answertypeid":"2"},{"QuestionID":"3","QuestionName":"you want extra service?","Questiontypeid":"1","Answertypeid":"3"},{"QuestionID":"4","QuestionName":"performance of quality ?","Questiontypeid":"1","Answertypeid":"4"},{"QuestionID":"5","QuestionName":"performance of staff?","Questiontypeid":"1","Answertypeid":"5"},{"QuestionID":"6","QuestionName":"when you left room ?","Questiontypeid":"2","Answertypeid":"1"},{"QuestionID":"7","QuestionName":"your words about roomservice ?","Questiontypeid":"2","Answertypeid":"2"},{"QuestionID":"8","QuestionName":"you like roomservice ?","Questiontypeid":"2","Answertypeid":"3"},{"QuestionID":"9","QuestionName":"performance room service ?","Questiontypeid":"2","Answertypeid":"4"},{"QuestionID":"10","QuestionName":"performance room service staff?","Questiontypeid":"2","Answertypeid":"5"}]
0

2 Answers 2

2

I think there are certain problems in your logic. For every JSON object u are creating new HashMap object inside for loop, so u will loose any previous data. Also HashMap will override new Data, so you will have only final data. What u can do is that create an arrayList of Hashmap....

     ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<STring, String>>();
    for (int i = 0; i<QjArray.length(); i++) {

           objJMap = new HashMap<String, String>(); 
........
           data.add(objJMap);
    ...}
Sign up to request clarification or add additional context in comments.

3 Comments

now json code in arraylist and now how to iterator that arraylist?
i finsh this now how to Iterate Arraylist ?how to use it??
u can find it easily, just search a bit
2

This is because your code re initializes the HashMap after every loop.

for (int i = 0; i<QjArray.length(); i++) {
             objJMap = new HashMap<String, String>(); 
....
}

Place the HashMap outside the loop and it will work fine.

objJMap = new HashMap<String, String>(); 
for (int i = 0; i<QjArray.length(); i++) {
....
}

Comments

Your Answer

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.