Java GSOn错误应为字符串,但在解析JSON响应时为BEGIN_对象
我从API检索JSON结果:Java GSOn错误应为字符串,但在解析JSON响应时为BEGIN_对象,java,android,json,gson,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": "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"
},...]
但是,当我尝试映射到客户对象时,我得到错误预期为字符串,但却是BEGIN\u 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);
}
关于这个问题,我已经找到了很多答案,对于其他类型的对象,我也找到了很多答案,但我找不到有效的解决方案。创建一个带有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);
公共类客户{
私有字符串oid;
私有字符串id;
私有字符串名称;
私有字符串时间戳;
私人字符串电子邮件;
修改私有字符串;
私有字符串url;
公共字符串getOid(){
返回oid;
}
公共void setOid(字符串oid){
this.oid=oid;
}
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getTimestamp(){
返回时间戳;
}
public void setTimestamp(字符串时间戳){
this.timestamp=时间戳;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getModified(){
修改返回值;
}
公共void setModified(字符串已修改){
this.modified=modified;
}
公共字符串getUrl(){
返回url;
}
公共void setUrl(字符串url){
this.url=url;
}
公共标识getLogo(){
返回标志;
}
公共标识(标识){
this.logo=logo;
}
私人标志;
}
公共类标志{
私有int型;
公共字符串getData(){
返回数据;
}
公共void setData(字符串数据){
这个数据=数据;
}
公共int getType(){
返回类型;
}
公共void集合类型(int类型){
this.type=type;
}
私有字符串数据;
}
Gson Gson=新的Gson();
类型listType=newTypeToken(){}.getType();
List customer=(List)gson.fromJson(jsonOutput,listType);
在您的Customer
类中,logo
字段是字符串吗?显示您的Customer
类请将您的Customer
类属性类型与JSON中的类型匹配。logo是字段字符串,但我添加了transient
关键字,因为我将存储路径,而不是imagelogo的类型为object。将类属性转换为对象,然后只保留路径。如果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);