开发者

get many value from json

I need to get latitude and longitude to my android, it got many values like this :

get many value from json

I tried this to get values from JSON :

ArrayList<String> longitude;
ArrayList<String> latitude;

public void parseJson(String result) {
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject jo = jArray.getJSONObject(i);
                latitude.add(jo.getString("Latitude").toString());
                longitude.add(jo.getString("Longitude").toString());
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
    }

But When I check the size of array, it's empty. Anyone can help or suggest my wrong ? Thanks

@after modified,I still got 0 size of array. So,I tried to search where it's stop by Toast then I found :

public void parseJson(String result) throws JSONException {

        longitude = new ArrayList<String>();
        latitu开发者_如何学Cde = new ArrayList<String>();

        JSONArray jArray = (JSONArray)JSONValue.parse(result);

        for (Object obj: jArray) {

            JSONObject jo = (JSONObject)obj;
            //here is when I toast it's got nothing
            latitude.add(jo.get("Latitude").toString());
            longitude.add(jo.get("Longitude").toString());
        }

    }

@aroth I got an answer!! I back to use my way like this :

public void parseJson(String result) throws JSONException {

        longitude = new ArrayList<String>();
        latitude = new ArrayList<String>();
        JSONArray jArray = new JSONArray(result);
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject jo = jArray.getJSONObject(i);
            latitude.add(jo.getString("Latitude").toString());
            longitude.add(jo.getString("Longitude").toString());
        }

I think I forgot declare these:

         longitude = new ArrayList<String>();
         latitude = new ArrayList<String>();

I still no idea why your code is not work. However, thank a lot for your help :))


The json.org library is needlessly obtuse with its get<Type> methods that all pointlessly throw JSONException, I would recommend switching to simple-json. Then you could do:

ArrayList<String> longitude;
ArrayList<String> latitude;

public void parseJson(String result) {
    longitude = new ArrayList<String>();
    latitude = new ArrayList<String>();
    JSONArray jArray = (JSONArray)JSONValue.parse(result);
    for (Object obj : jArray) {
        JSONObject jo = (JSONObject)obj;
        latitude.add(jo.get("Latitude").toString());
        longitude.add(jo.get("Longitude").toString());
    }

}

//a simple test case
public static void main(String[] args) {
    String json = "[{\"Latitude\": \"1.23\", \"Longitude\": \"1.23\"}, \n{\"Latitude\": \"1.23\", \"Longitude\": \"1.23\"}, \n{\"Latitude\": \"1.23\", \"Longitude\": \"1.23\"}]";    
    MyParser parser = new MyParser();
    parser.parseJson(json);

    //should print:  3, 3
    System.out.println(parser.latitude.size() + ", " + parser.longitude.size());
}

Should work, assuming your input JSON matches your example snippet.


try using gson library. It's compact and fast.

https://sites.google.com/site/gson/gson-user-guide

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜