在Android中将JSON转换为HashMap的问题

在Android中将JSON转换为HashMap的问题,android,json,hashmap,type-conversion,Android,Json,Hashmap,Type Conversion,我有JSONArray当我解码JSON到HashMap时,当时HashMap取JSONArray的最后一个值 这是我的代码: QjArray = new JSONArray(Questionresult); JSONObject json_data = new JSONObject(); for (int i = 0; i<QjArray.length(); i++) { objJMap = new HashMap<String, String>(); js

我有
JSONArray
当我解码
JSON
HashMap
时,当时
HashMap
JSONArray
的最后一个值

这是我的代码:

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);
}

这是因为代码在每次循环后都会重新初始化HashMap

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

for(inti=0;i这是因为您的代码在每次循环后都会重新初始化HashMap

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

for(int i=0;i我认为您的逻辑中存在某些问题。对于每个JSON对象,您在for循环中创建新的HashMap对象,因此您将丢失任何以前的数据。此外,HashMap将覆盖新数据,因此您将只拥有最终数据。您可以做的是创建HashMap的arrayList。。。。


ArrayList data=new ArrayList();

对于(int i=0;i我认为您的逻辑中存在某些问题。对于每个JSON对象,您在for循环中创建新的HashMap对象,因此您将丢失任何以前的数据。此外,HashMap将覆盖新数据,因此您将只拥有最终数据。您可以做的是创建HashMap的arrayList。。。。

ArrayList data=new ArrayList();

对于(int i=0;在arraylist中输入新的json代码,现在如何迭代该arraylist?我现在知道该如何迭代arraylist?如何使用它??您可以很容易地找到它,只需在arraylist中搜索bitnow json代码,现在如何迭代该arraylist?我现在知道该如何迭代arraylist?如何使用它??您可以很容易地找到它,只需搜索一点
objJMap = new HashMap<String, String>(); 
for (int i = 0; i<QjArray.length(); i++) {
....
}
     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);
    ...}