GSOn错误在分析JSON响应时,预期有一个字符串,但是是BEGIN_OBJECT [英] GSOn error Expected a string but was BEGIN_OBJECT when parsing JSON response

查看:131
本文介绍了GSOn错误在分析JSON响应时,预期有一个字符串,但是是BEGIN_OBJECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从API中检索JSON结果:

  [{
oid:axd7wtlk6xd2fbwlc5wk,
id:aazzzza,
name:aazzaa,
logo:{
type:0,
data :iVB ............. 5CYII =

时间戳:1438608571013,
电子邮件:contact@azzaa.net ,
modified:test,
url:http://www.azzaa.net
},
{
oid :quj3dzygfwygl5uxsbxk,
name:KZZZ,
modified:test,
timestamp:1438854099511,
id:kess
},...]

但是当我尝试映射到客户对象I得到错误预期一个字符串,但是BEGIN_OBJECT

  response = webService .RequestGet(url,header); 

result = null;
尝试{
result = new JSONArray(response);
Utils.LogWarning(response);
} catch(JSONException e){
Utils.LogError(无法加载json响应,e);
}

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

这是我的 Customer class:

  public class Customer implements Serializable {
private String id =;
private String name =;
private String email =;
private String url =;
private String address =;
private String stamp =;
//私有瞬态String logo =;
私人长时间戳= 0L;
private String modified =;
...

}

<我已经通过了很多关于这个问题的答案,我也为其他类型的对象,但我找不到一个工作的解决方案。

解决方案

使用JSON结果的值创建一个模式,比如

  public class Customer {
private String oid;
私人字符串ID;
私人字符串名称;
private String timestamp;
私人字符串电子邮件;
私人字符串修改;
私人字符串网址;

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;
}

私人标志徽标;
}

public class Logo {
private int type;

public String getData(){
返回数据;
}

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

public int getType(){
返回类型;
}

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

私有字符串数据;
}

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


I retrieve a JSON result from an API :

[{
"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"
},...]

but when I try to map to a customer object I get the error 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);

Here is my Customer class :

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 = "";
    ...

}

I have been through a lot of answers regarding this problem that I have also for other types of objects, but I can't find a working solution.

解决方案

Create a modal with the values of JSON result like

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

这篇关于GSOn错误在分析JSON响应时,预期有一个字符串,但是是BEGIN_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆