java - GSOn 错误 解析 JSON 响应时需要一个字符串,但结果是 BEGIN_OBJECT

标签 java android json gson

我从 API 检索 JSON 结果:

[{
"oid": "axd7wtlk6xd2fbwlc5wk",
"id": "aazzzza",
"name": "aazzaa",
"logo": {
"type": 0,
"data": "iVB.............5CYII="
},
"timestamp": 1438608571013,
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22414d4c564341566243585843430c4c4756" rel="noreferrer noopener nofollow">[email protected]</a>",
"modified": "test",
"url": "http://www.azzaa.net"
},
{
"oid": "quj3dzygfwygl5uxsbxk",
"name": "KZZZ",
"modified": "test",
"timestamp": 1438854099511,
"id": "kess"
},...]

但是当我尝试映射到客户对象时,我收到错误期望一个字符串,但结果是 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 = "";
    ...

}

我已经针对这个问题找到了很多答案,对于其他类型的对象也有同样的答案,但我找不到有效的解决方案。

最佳答案

使用 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);

关于java - GSOn 错误 解析 JSON 响应时需要一个字符串,但结果是 BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32069021/

相关文章:

java - 如何从Tomcat中的JDBC连接池中释放连接?

java - JSF:MyFaces CODI 类型安全导航和 f:viewParam

java - 使用 jpa 删除 map 的元素

android - Osmdroid 离线模式在缩放后更新图 block

javascript - Facebook Graph 获取全名

javascript - 合并2个数组的对象

java - 使用 LibGdx 在 Java 中出现奇怪的 CPU 使用率

android - 重置 Android 计步器

android - ListActivity ormlite 问题?

搜索 ArrayList 时发生 Java JSON ClassCastException