JSONException:值存在时没有值

时间:2016-03-16 12:13:56

标签: android json getstring jsonexception

我循环访问JSON数组,意图将详细信息保存到数据库中。

当我到达以下JSON时:

{"custom":"0","is_affected":"1","title":"Up Goods","direction":"","affect_type":"O","code":"0","speed":""}

我收到以下异常消息:

org.json.JSONException: No value for direction

我正在使用的代码在这里:

public static final String KEY_CUSTOM = "custom";
public static final String KEY_IS_AFFECTED = "is_affected";
public static final String KEY_TITLE = "title";
public static final String KEY_DIRECTION = "direction";
public static final String KEY_AFFECT_TYPE = "affect_type";
public static final String KEY_CODE = "code";
public static final String KEY_SPEED = "speed";

public void saveRT9909(Context context, JSONObject object){
    try{
        String html = object.getString("response");
        JSONObject jObj = new JSONObject(html);
        String action = jObj.getString("action");
        String response = jObj.getString("response");
        JSONObject resp = new JSONObject(response);

        String rt9909 = resp.getString("DataPackRt9909");

        JSONObject rt = new JSONObject(rt9909);

        String pack_id = rt.getString(DB.KEY_PACK_ID);

        String site_wardens = rt.getString(DB.KEY_SITE_WARDENS);

        String declarations = rt.getString(DB.KEY_DECLARATIONS);

        String[][] rt9909Values = {
                {DB.KEYLOWERID, rt.getString(DB.KEYLOWERID)},
                {DB.KEY_PACK_ID, rt.getString(DB.KEY_PACK_ID)},
                {DB.KEY_FORM_INDEX, rt.getString(DB.KEY_FORM_INDEX)},
                {DB.KEY_COSS_NAME, rt.getString(DB.KEY_COSS_NAME)},
                {DB.KEY_NAME_OF_COSS, rt.getString(DB.KEY_NAME_OF_COSS)},
                {DB.KEY_COSS_ID, rt.getString(DB.KEY_COSS_ID)},
                {DB.KEY_SENTINEL_CARD_NO, rt.getString(DB.KEY_SENTINEL_CARD_NO)},
                {DB.KEY_PLANNED_START_TIME, rt.getString(DB.KEY_PLANNED_START_TIME)},
                {DB.KEY_PLANNED_END_TIME, rt.getString(DB.KEY_PLANNED_END_TIME)},
                {DB.KEY_ACTUAL_START_TIME, rt.getString(DB.KEY_ACTUAL_START_TIME)},
                {DB.KEY_ACTUAL_END_TIME, rt.getString(DB.KEY_ACTUAL_END_TIME)},
                {DB.KEY_PERMIT_TO_WORK, rt.getString(DB.KEY_PERMIT_TO_WORK)},
                {DB.KEY_PLANNED_WALKING, rt.getString(DB.KEY_PLANNED_WALKING_ACTUAL)},
                {DB.KEY_PLANNED_WORK, rt.getString(DB.KEY_PLANNED_WORK)},
                {DB.KEY_PLANNED_WORK_ACTUAL, rt.getString(DB.KEY_PLANNED_WORK_ACTUAL)},
                {DB.KEY_TYPE_OF_FENCE, rt.getString(DB.KEY_TYPE_OF_FENCE)},
                {DB.KEY_DISTANCE_FROM_LINE, rt.getString(DB.KEY_DISTANCE_FROM_LINE)},
                {DB.KEY_SEPARATION_DISTANCE, rt.getString(DB.KEY_SEPARATION_DISTANCE)},
                {DB.KEY_COSS_SITE_WARNING, rt.getString(DB.KEY_COSS_SITE_WARNING)},
                {DB.KEY_SITE_WARDEN_WARNING, rt.getString(DB.KEY_SITE_WARDEN_WARNING)},
                {DB.KEY_RED_ZONE_WARNING, rt.getString(DB.KEY_RED_ZONE_WARNING)},
                {DB.KEY_SAFETY_LOCATIONS, rt.getString(DB.KEY_SAFETY_LOCATIONS)},
                {DB.KEY_ACCESS_EGRESS, rt.getString(DB.KEY_ACCESS_EGRESS)},
                {DB.KEY_HAZARDS_SITE, rt.getString(DB.KEY_HAZARDS_SITE)},
                {DB.KEY_ADDITIONAL_INFO, rt.getString(DB.KEY_ADDITIONAL_INFO)},
                {DB.KEY_LIMITS_DEFINITION, rt.getString(DB.KEY_LIMITS_DEFINITION)},
                {DB.KEY_ADD_SIGHTING_CHARTS, rt.getString(DB.KEY_ADD_SIGHTING_CHARTS)},
                {DB.KEY_PACK_SITE_LOCATION_CUSTOM, rt.getString(DB.KEY_PACK_SITE_LOCATION_CUSTOM)},
                {DB.KEY_COSS_SIGNATURE_ID, rt.getString(DB.KEY_COSS_SIGNATURE_ID)},
                {DB.KEY_NEAREST_LINE_CODE, rt.getString(DB.KEY_NEAREST_LINE_CODE)},
                {DB.KEY_CHANGE_REASON, rt.getString(DB.KEY_CHANGE_MANAGER)},
                {DB.KEY_CHANGE_AUTHORITY_NO, rt.getString(DB.KEY_CHANGE_AUTHORITY_NO)}
        };


        String pack_linesAff = rt.getString(DB.KEY_PACK_LINES_AFFECTED);

        JSONArray plaArr = new JSONArray(pack_linesAff);


        for (int n =0; n < plaArr.length(); n++){
            JSONObject pla = plaArr.getJSONObject(n);

            String[][] plaValues = {
                    {DB.KEY_PACK_ID, pack_id},
                    {DB.KEY_CUSTOM, pla.getString(DB.KEY_CUSTOM)},
                    {DB.KEY_IS_AFFECTED, pla.getString(DB.KEY_IS_AFFECTED)},
                    {DB.KEY_TITLE, pla.getString(DB.KEY_TITLE)},
                    {DB.KEY_DIRECTION, pla.getString(DB.KEY_DIRECTION)},
                    {DB.KEY_AFFECT_TYPE, pla.getString(DB.KEY_AFFECT_TYPE)},
                    {DB.KEY_CODE, pla.getString(DB.KEY_CODE)},
                    {DB.KEY_SPEED, pla.getString(DB.KEY_SPEED)}
            };

            Log.d("RT9909", pla+"");
        }



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

此外,如果我对方向的值进行硬编码,那么我会得到与affect_type相同的异常。

JSON响应 enter image description here

3 个答案:

答案 0 :(得分:1)

只需将rt.getString(key)替换为rt.optString(key)

即可

答案 1 :(得分:0)

似乎JSONObject.getString函数在解释字符串时正在查找键值对中键的值,如果找不到则会抛出异常。

使用JSONObject.isNull()函数检查键值对中的值是否存在,然后再将其映射到数组。见下文:

http://developer.android.com/reference/org/json/JSONObject.html

另请注意,它提到如果找不到值,getString函数将抛出异常。

编辑:JSONObject.isNull而不是JSONObject.has

答案 2 :(得分:0)

某些json对象缺少字段。 例如

您正确的json对象

{"custom":"0","is_affected":"1","title":"Up Goods","direction":"","affect_type":"O","code":"0","speed":""}

您的json对象缺少字段:

{"custom":"0","is_affected":"1","title":"Up Goods"}

所以当你调用jsonObject.getString(“direction”)时会抛出Exception。

在获取之前,您可以更安全地检查您的字段的实例/现有。

例如

private String optString(JSONObject jsonObject, String key) {
    return jsonObject.isNull(key) ? null : jsonObject.optString(key);
}