首页 > gson解析报错

gson解析报错

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" +
            "}";

请问这到底是为什么啊??


你先检查一下你的json数据是否是合法的


用gson-format工具自动生成bean


建议的测试做法不是本地通过字符串去拼接进行测试,而是直接通过在线的JSON校验工具,优先校验下json数据。
看后端或者其他来源提供的json串的正确性,然后再去比对GSON解析错误的原因。


你的数据格式不对,少逗号

\"illness_name\":\"功能神经外科\",

你这很明显对应不上啊, Doctor 是jsonarray,上面还有一层呢,error和results呢,这一层你没有对应的response啊。

【热门文章】
【热门文章】