GSOn错误在解析JSON响应

时间:2015-08-18 09:48:46

标签: java android json gson

我从API检索JSON结果:

[{
"oid": "axd7wtlk6xd2fbwlc5wk",
"id": "aazzzza",
"name": "aazzaa",
"logo": {
"type": 0,
"data": "iVB.............5CYII="
},
"timestamp": 1438608571013,
"email": "contact@azzaa.net",
"modified": "test",
"url": "http://www.azzaa.net"
},
{
"oid": "quj3dzygfwygl5uxsbxk",
"name": "KZZZ",
"modified": "test",
"timestamp": 1438854099511,
"id": "kess"
},...]

但是当我尝试映射到客户对象时,我收到错误Expected a string but was BEGIN_OBJECT

    response = webService.RequestGet(url, header);

    result = null;
    try {
        result = new JSONArray(response);
        Utils.LogWarning(response);
    } catch (JSONException e) {
        Utils.LogError("Could not load json response", e);
    }

    Type customerType = new TypeToken<Collection<Customer>>() {
    }.getType();
    ArrayList<Customer> alCustomers = null;
    alCustomers = new Gson().fromJson(result.toString(), customerType);

这是我的Customer课程:

public class Customer implements Serializable {
    private String id = "";
    private String name = "";
    private String email = "";
    private String url = "";
    private String address = "";
    private String stamp = "";
    //private transient String logo = "";
    private long timestamp = 0L;
    private String modified = "";
    ...

}

我已经针对这个问题做了很多回答,我也有其他类型的对象,但我找不到合适的解决方案。

1 个答案:

答案 0 :(得分:2)

创建一个具有JSON结果值的模态,如

    public class Customer {
        private String oid;
        private String id;
        private String name;
        private String timestamp;
        private String email;
        private String modified;
        private String url;

        public String getOid() {
            return oid;
        }

        public void setOid(String oid) {
            this.oid = oid;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getModified() {
            return modified;
        }

        public void setModified(String modified) {
            this.modified = modified;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public Logo getLogo() {
            return logo;
        }

        public void setLogo(Logo logo) {
            this.logo = logo;
        }

        private Logo logo;
    }

    public class Logo {
        private int type;

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        private String data;
    }

  Gson gson = new Gson();
 Type listType = new TypeToken<List<Customer>>(){}.getType();
List<Customer> customer= (List<Customer>) gson.fromJson(jsonOutput, listType);