我正在循环一个 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 的相同异常。