0

我正在循环一个 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();
    }
}

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

JSON 响应 在此处输入图像描述

4

3 回答 3

3

只需更换rt.getString(key)rt.optString(key)

于 2016-03-16T12:34:23.477 回答
0

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

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

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

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

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

于 2016-03-16T12:23:17.807 回答
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") 时会抛出异常。

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

例如

private String optString(JSONObject jsonObject, String key) {
    return jsonObject.isNull(key) ? null : jsonObject.optString(key);
}
于 2016-03-16T12:33:42.157 回答