尝试获取jsonarray时出现JSON错误

时间:2016-03-22 15:16:07

标签: android arrays json

我想从json对象获取数组但是得到错误

当我做jarray2 = object1.getJSONArray(route);

我收到以下错误

APP CRASH:(以下JSON)没有值

[{
    "options": {
        "allowUTurn": false
    },
    "latLng": {
        "lat": 52.42252295423907,
        "lng": 4.873809814453125
    },
    "name": "Coenplein, A10, Amsterdam, Noord, Amsterdam, MRA, Stadsregio Amsterdam, North Holland, Netherlands, 1035RN, The Netherlands",
    "_initHooksCalled": true
}, {
    "options": {
        "allowUTurn": false
    },
    "latLng": {
        "lat": 52.28674750920209,
        "lng": 4.941873550415039
    },
    "name": "Holendrechterzijweg, Ouderkerk aan de Amstel, Ouder-Amstel, MRA, Stadsregio Amsterdam, North Holland, Netherlands, 1191LJ, The Netherlands",
    "_initHooksCalled": true
}, {
    "options": {
        "allowUTurn": false
    },
    "latLng": {
        "lat": 52.0727536539532,
        "lng": 4.32861328125
    },
    "name": "87, Zwetstraat, Rivierenbuurt, The Hague, MRDH, South Holland, Netherlands, 2515VE, The Netherlands",
    "_initHooksCalled": true
}]

这是代码

try
    {
        object1 = new JSONObject(response);
        jarray1 = object1.getJSONArray("selected_route_data");
        for(int i=0;i<jarray1.length();i++)
        {
            json1 = jarray1.getJSONObject(i);
            String route_id = json1.getString("route_id").toString();
            String route_name = json1.getString("route_name").toString();
            String route = json1.getString("route_data").toString();

            jarray2 = object1.getJSONArray(route);

        }
    }
    catch (JSONException e)
    {
        Log.e("APP CRASH", e.getMessage());

    }

2 个答案:

答案 0 :(得分:2)

您尝试解析的JSON似乎不包含您要查找的值。数组中的所有元素都不包含route_idroute_nameroute_data

答案 1 :(得分:0)

您应该检查您正在解析的JSON中是否存在元素,您可以执行类似

的操作
{{1}}