com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但在android中是BEGIN_OBJECT

时间:2015-08-13 09:03:49

标签: android json gson illegalstateexception

我正在尝试使用bean类解析Json数据,如下所示,但我收到错误" com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但是BEGIN_OBJECT"

JSON数据

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

}

LaboratoryListModel

public class LaboratoryListModel {

{
"response": true,
"message": "Success",
"Data": [
    {
        "laboratory_id": 1000,
        "laboratory_image": "http://abcd.com/images/laboratory.jpg",
        "laboratory_name": "Laboratory & Research Institute",
        "laboratory_address": "Cross Road, ",
        "laboratory_rating": 2.5,
        "laboratory_profile": {
            "head_name": "Ramesh Jain",
            "degree_name": "M.D",
            "speciality": "Lab Technology",
            "license_number": "34343434",
            "laboratory": "Laboratory"
        }
    },
    {
        "laboratory_id": 1001,
        "laboratory_image": "http://abcd.com/images/laboratory.jpg",
        "laboratory_name": "XYZ LAB",
        "laboratory_address": "ASHKA ROAD",
        "laboratory_rating": 0,
        "laboratory_profile": {
            "head_name": "MR X",
            "degree_name": "MBBS",
            "speciality": null,
            "license_number": "123456",
            "laboratory": "Diagnostic"
        }
    }

]

}

LaboratoryList
    公共类LaboratoryList {

private String response;
private String message;
private ArrayList<LaboratoryList> Data = new ArrayList<LaboratoryList>();


public String getResponse() {
    return response;
}

public void setResponse(String response) {
    this.response = response;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public ArrayList<LaboratoryList> getData() {
    return Data;
}

public void setData(ArrayList<LaboratoryList> data) {
    Data = data;
}

}

LaboratoryProfile

private String laboratory_id;
private String laboratory_image;
private String laboratory_name;
private String laboratory_address;
private String laboratory_rating;

private ArrayList<LaboratoryProfile> laboratory_profile = new ArrayList<LaboratoryProfile>();

public ArrayList<LaboratoryProfile> getLaboratory_profile() {
    return laboratory_profile;
}

public void setLaboratory_profile(
        ArrayList<LaboratoryProfile> laboratory_profile) {
    this.laboratory_profile = laboratory_profile;
}

public String getLaboratory_id() {
    return laboratory_id;
}

public void setLaboratory_id(String laboratory_id) {
    this.laboratory_id = laboratory_id;
}

public String getLaboratory_image() {
    return laboratory_image;
}

public void setLaboratory_image(String laboratory_image) {
    this.laboratory_image = laboratory_image;
}

public String getLaboratory_name() {
    return laboratory_name;
}

public void setLaboratory_name(String laboratory_name) {
    this.laboratory_name = laboratory_name;
}

public String getLaboratory_address() {
    return laboratory_address;
}

public void setLaboratory_address(String laboratory_address) {
    this.laboratory_address = laboratory_address;
}

public String getLaboratory_rating() {
    return laboratory_rating;
}

public void setLaboratory_rating(String laboratory_rating) {
    this.laboratory_rating = laboratory_rating;
}

}

web服务

public class LaboratoryProfile {

private String head_name;
private String degree_name;
private String speciality;
private String license_number;
private String laboratory;

public String getHead_name() {
    return head_name;
}

public void setHead_name(String head_name) {
    this.head_name = head_name;
}

public String getDegree_name() {
    return degree_name;
}

public void setDegree_name(String degree_name) {
    this.degree_name = degree_name;
}

public String getSpeciality() {
    return speciality;
}

public void setSpeciality(String speciality) {
    this.speciality = speciality;
}

public String getLicense_number() {
    return license_number;
}

public void setLicense_number(String license_number) {
    this.license_number = license_number;
}

public String getLaboratory() {
    return laboratory;
}

public void setLaboratory(String laboratory) {
    this.laboratory = laboratory;
}

由于我是新手,我不知道问题是什么,我做谷歌但我没有得到解决方案,有人可以帮我吗?

3 个答案:

答案 0 :(得分:1)

更改您的LaboratoryList类行

private ArrayList<LaboratoryProfile> laboratory_profile = new ArrayList<LaboratoryProfile>();

到这个

private LaboratoryProfile laboratory_profile;

并使用新的getter和setter删除以前的getter和setter。你已经完成了

答案 1 :(得分:1)

关于你的评论。你需要改变你的模型。您的LaboratoryList应该只包含一个配置文件。

将其更改为:

private LaboratoryProfile laboratory_profile;

public LaboratoryProfile getLaboratory_profile() {
    return laboratory_profile;
}

答案 2 :(得分:0)

在您的LaboratoryList.java中,您有一个配置文件列表,但您的JSON不代表这一点。它是一个JSON对象。将其更改为数组或仅将配置文件列表设为一个配置文件。

您的个人资料JSON应如下所示:

"laboratory_profile":[  
        {  
           "head_name":"MR X",
           "degree_name":"MBBS",
           "speciality":null,
           "license_number":"123456",
           "laboratory":"Diagnostic"
        }
     ]