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" +
"}";
请问这到底是为什么啊??
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.
Your data format is wrong, missing commas
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.
You first check whether your json data is legal
Use gson-format tool to automatically generate beans