问题描述:
提示:Android中解析JSONObject拿到的null数据成"null",并在TextView中显示null字符串。
JSONObject survey = data.getJSONObject("data").getJSONArray("survey").getJSONObject(0);
SurveyModel model = new SurveyModel();
model.setRecord_id(survey.getInt("record_id"));
model.setStamp(survey.getInt("stamp"));
model.setStart_date(survey.getString("start_date"));
model.setEnd_date(survey.getString("end_date"));
model.setTitle(survey.getString("title"));
model.setTitle_tc(survey.getString("title_tc"));
model.setDescription(survey.isNull("description")?null:survey.getString("description"));
model.setDescription_tc(survey.isNull("description_tc")?null:survey.getString("description_tc"));
model.setQuestions(survey.getJSONArray("form_format"));
fragment = QuestionPreviewFragment.newInstance(model);
原因分析:
JSONObject处理数据bug,处理null数据会被存为一个JSONObject对象"null"。
解决方案:
方案一:用Gson解析数据
方案二:加入判断,见上代码
方案三(不推荐):第一次拿到json数据时,将数据中的null字符串替换为""。
str.replaceAll(“null”,"");