RT,在用gson解析的时候报错 Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 7 column 8 path $.results[0].illness_name
我的doctor类定义如下
*/
public class Doctor {
private int id;
private String illness_name;
private String doctor_name;
private String hospital_name;
private String head_image;
private String personal_website;
private String doctor_intro;
解析gson的代码如下
private void initData() {
Gson gson = new Gson();
Type userType = new TypeToken<ResponseResult<List<Doctor>>>() {
}.getType();
ResponseResult<List<Doctor>> result = gson.fromJson(results, userType);
Log.d("result","-------------->"+results);
System.out.println("-----------------"+results);
doctors = result.getResults();
final DoctorListAdapter doctorListAdapter = new DoctorListAdapter(DoctorListActivity.this, (ArrayList<Doctor>) doctors,this);
lv_doctors.setAdapter(doctorListAdapter);
lv_doctors.setSelection(0);
}
json数据如下
private static final String results = "{\n" +
" \"error\": false,\n" +
" \"results\": [\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"illness_name\":\"功能神经外科\"\n" +
" \"doctor_name\":\"胡勇生\"\n" +
" \"hospital_name\":\"玄武医院\"\n" +
" \"head_image\":\"http://...\"\n" +
" \"personal_website\":\"http://...\"\n" +
" \"doctor_intro\":\"擅长:三叉神经痛。。。。。。。。。。。\"\n" +
" }\n" +
" ]\n" +
"}";
请问这到底是为什么啊??
阿神2017-04-17 17:33:47
建议的测试做法不是本地通过字符串去拼接进行测试,而是直接通过在线的JSON校验工具,优先校验下json数据。
看后端或者其他来源提供的json串的正确性,然后再去比对GSON解析错误的原因。