android - gson解析报错
ringa_lee
ringa_lee 2017-04-17 17:31:47
0
5
550

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

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

ringa_lee
ringa_lee

ringa_lee

全部回复(5)
Peter_Zhu

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

黄舟

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

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

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

左手右手慢动作

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

小葫芦

用gson-format工具自动生成bean

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板