android - gson解析报错

 叶肖帆Seantq_693 发布于 2022-10-29 20:06

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>>() {
        }.getType();
        ResponseResult> result = gson.fromJson(results, userType);
        Log.d("result","-------------->"+results);
        System.out.println("-----------------"+results);
        doctors = result.getResults();

        final DoctorListAdapter doctorListAdapter = new DoctorListAdapter(DoctorListActivity.this, (ArrayList) 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" +
            "}";

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

5 个回答
  • 用gson-format工具自动生成bean

    2022-10-31 19:31 回答
  • 你先检查一下你的json数据是否是合法的

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

    2022-10-31 19:32 回答
  • 你的数据格式不对,少逗号

    \"illness_name\":\"功能神经外科\",
    2022-10-31 19:33 回答
  • 你这很明显对应不上啊, Doctor 是jsonarray,上面还有一层呢,error和results呢,这一层你没有对应的response啊。

    2022-10-31 19:33 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有