2014-11-08 90 views
1

我想解析一个JSONArray到和我的Android应用程序中的ArrayList。 PHP脚本正确retuns预期的结果,但是Java的失败,一个空指针异常的resultsList.add(map)Android的JSONArray ArrayList

public void agencySearch(String tsearch) { 
     // Setting the URL for the Search by Town 
     String url_search_agency = "http://www.infinitycodeservices.com/get_agency_by_city.php"; 
     // Building parameters for the search 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("City", tsearch)); 

     // Getting JSON string from URL 
     JSONArray json = jParser.getJSONFromUrl(url_search_agency, params); 

     for (int i = 0; i < json.length(); i++) { 
      HashMap<String, String> map = new HashMap<String, String>(); 

      try { 
       JSONObject c = (JSONObject) json.get(i); 
       //Fill map 
       Iterator iter = c.keys(); 
       while(iter.hasNext()) { 
        String currentKey = (String) iter.next(); 
        map.put(currentKey, c.getString(currentKey)); 
       } 
       resultsList.add(map); 

      } 
      catch (JSONException e) { 
       e.printStackTrace(); 

      } 

     }; 

     MainActivity.setResultsList(resultsList); 

    } 
+0

,你初始化'resultsList'在你的代码? – 2014-11-08 07:07:59

+0

这是因为'resultsList'为空。 – immibis 2014-11-08 07:19:57

+0

你有初始化resultList? – micky 2014-11-08 07:20:26

回答

1

尝试这样可以帮助你,这将您JSONArray列出

public void agencySearch(String tsearch) { 
     // Setting the URL for the Search by Town 
     String url_search_agency = "http://www.infinitycodeservices.com/get_agency_by_city.php"; 
     // Building parameters for the search 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("City", tsearch)); 

     // Getting JSON string from URL 
     JSONArray json = jParser.getJSONFromUrl(url_search_agency, params); 

     ArrayList<HashMap<String, String>> resultsList = new ArrayList<HashMap<String, String>>(); 

     for (int i = 0; i < json.length(); i++) { 
      HashMap<String, String> map = new HashMap<String, String>(); 

      try { 
       JSONObject c = json.getJSONObject(position); 
       //Fill map 
       Iterator<String> iter = c.keys(); 
       while(iter.hasNext()) { 
        String currentKey = it.next(); 
        map.put(currentKey, c.getString(currentKey)); 
       } 
       resultsList.add(map); 

      } 
      catch (JSONException e) { 
       e.printStackTrace(); 

      } 

     }; 

     MainActivity.setResultsList(resultsList); 

    } 
+1

你能指出你改变了什么吗? – 2014-11-08 07:11:09

+0

好的。我的resultsList声明在类代码中的其他地方。这是我最初的定义ArrayList > resultsList;将= new ArrayList >()添加到该行的末尾后,异常消失。再次感谢。也许我应该在问这个问题之前先睡一会儿再检查一遍。简单的语法错误。希望它能帮助别人。 – IrishCarBomb 2014-11-08 13:25:45

1

使用自定义的方法而不是迭代和建立List。

如何拨打:

try { 
    ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String,String>>) toList(json); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

转换JSON数组列表:

private List toList(JSONArray array) throws JSONException { 
    List list = new ArrayList(); 
    int size = array.length(); 
    for (int i = 0; i < size; i++) { 
     list.add(fromJson(array.get(i))); 
    } 
    return list; 
} 

转换JSON到对象:

private Object fromJson(Object json) throws JSONException { 
    if (json == JSONObject.NULL) { 
     return null; 
    } else if (json instanceof JSONObject) { 
     return jsonToMap((JSONObject) json); 
    } else if (json instanceof JSONArray) { 
     return toList((JSONArray) json); 
    } else { 
     return json; 
    } 
} 

转换JSON映射:

public Map<String, String> jsonToMap(JSONObject object) throws JSONException { 
    Map<String, String> map = new HashMap(); 
    Iterator keys = object.keys(); 
    while (keys.hasNext()) { 
     String key = (String) keys.next(); 
     map.put(key, fromJson(object.get(key)).toString()); 
    } 
    return map; 
} 
+0

你有没有试试这个@IrishCarBomb? – 2014-11-08 09:30:41

+0

我没有。这是使用GSON LIB吗? – IrishCarBomb 2014-11-08 13:28:12

+0

不!它不使用GSON LIB。 – 2014-11-08 13:29:33