search

Home  >  Q&A  >  body text

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

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

ringa_leeringa_lee2772 days ago548

reply all(5)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 17:33:47

    You obviously don't correspond to this. Doctor is a jsonarray, and there is another layer above it. As for errors and results, you don't have a corresponding response for this layer.

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:33:47

    Your data format is wrong, missing commas

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

    reply
    0
  • 阿神

    阿神2017-04-17 17:33:47

    The recommended testing method is not to test by splicing strings locally, but directly through the online JSON verification tool, giving priority to verifying the json data.
    Check the correctness of the json string provided by the backend or other sources, and then compare the cause of the GSON parsing error.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:33:47

    You first check whether your json data is legal

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:33:47

    Use gson-format tool to automatically generate beans

    reply
    0
  • Cancelreply