2014-11-08 94 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