Android JSONArray to ArrayList

旧时模样 提交于 2020-01-11 07:16:24

问题


I am trying to parse a JSONArray into and ArrayList in my android app. The PHP script correctly retuns the expected results, however the Java fails with a null pointer exception at resultsList.add(map)

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

    }

回答1:


try like this may help you,

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

    }



回答2:


Use custom method which convert your JSONArray to List instead of iterate and build List.

How to call :

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

Convert json array to List :

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

Convert json to Object :

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

Convert json to map :

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


来源:https://stackoverflow.com/questions/26814673/android-jsonarray-to-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!