首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试对简单rest api响应进行配对时出现GSON抛出错误:应为BEGIN_ARRAY,但在第1行、第2列、路径$处为BEGIN_OBJECT

尝试对简单rest api响应进行配对时出现GSON抛出错误:应为BEGIN_ARRAY,但在第1行、第2列、路径$处为BEGIN_OBJECT
EN

Stack Overflow用户
提问于 2019-09-09 19:13:55
回答 1查看 26关注 0票数 2

我正在尝试像这样解析JSON

代码语言:javascript
复制
{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 1
    },
    "objects": [
        {
            "customer_id": "some-customer-id",
            "id": 5,
            "is_active": true,
            "product_code": "some-product-code",
            "resource_uri": "/api/v1/aws-marketplace/5/",
            "support_subscription_id": "22"
        }
    ]
}

http://www.jsonschema2pojo.org生成我得到了他的Java类

代码语言:javascript
复制
package com.greenqloud.netappstats.collector.metrics.gqconsole.api;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class AwsMarketplace {

@SerializedName("meta") 
@Expose
private Meta meta;
@SerializedName("objects")
@Expose
private List<Object> objects = null;

public Meta getMeta() {
return meta;
}

public void setMeta(Meta meta) {
this.meta = meta;
}

public List<Object> getObjects() {
return objects;
}

public void setObjects(List<Object> objects) {
this.objects = objects;
}

}

Meta类

代码语言:javascript
复制
package com.greenqloud.netappstats.collector.metrics.gqconsole.api;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Meta {

@SerializedName("limit")
@Expose
private int limit;
@SerializedName("next")
@Expose
private String next;
@SerializedName("offset")
@Expose
private int offset;
@SerializedName("previous")
@Expose
private String previous;
@SerializedName("total_count")
@Expose
private int totalCount;

public int getLimit() {
return limit;
}

public void setLimit(int limit) {
this.limit = limit;
}

public String getNext() {
return next;
}

public void setNext(String next) {
this.next = next;
}

public int getOffset() {
return offset;
}

public void setOffset(int offset) {
this.offset = offset;
}

public String getPrevious() {
return previous;
}

public void setPrevious(String previous) {
this.previous = previous;
}

public int getTotalCount() {
return totalCount;
}

public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}

}

对象类

代码语言:javascript
复制
package com.greenqloud.netappstats.collector.metrics.gqconsole.api;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Object {

@SerializedName("customer_id")
@Expose
private String customerId;
@SerializedName("id")
@Expose
private int id;
@SerializedName("is_active")
@Expose
private boolean isActive;
@SerializedName("product_code")
@Expose
private String productCode;
@SerializedName("resource_uri")
@Expose
private String resourceUri;
@SerializedName("support_subscription_id")
@Expose
private String supportSubscriptionId;

public String getCustomerId() {
return customerId;
}

public void setCustomerId(String customerId) {
this.customerId = customerId;
}

public int getId() {
return id;
}

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

public boolean isIsActive() {
return isActive;
}

public void setIsActive(boolean isActive) {
this.isActive = isActive;
}

public String getProductCode() {
return productCode;
}

public void setProductCode(String productCode) {
this.productCode = productCode;
}

public String getResourceUri() {
return resourceUri;
}

public void setResourceUri(String resourceUri) {
this.resourceUri = resourceUri;
}

public String getSupportSubscriptionId() {
return supportSubscriptionId;
}

public void setSupportSubscriptionId(String supportSubscriptionId) {
this.supportSubscriptionId = supportSubscriptionId;
}

}

但在尝试使用此函数进行反序列化时

代码语言:javascript
复制
Type localVarReturnType = new TypeToken<List<InstanceCreatorForAwsMarketplace>>(){}.getType();
ApiResponse<List<InstanceCreatorForAwsMarketplace>> responseFromApiCleint = apiClient.execute(newCall, localVarReturnType);

我得到以下错误:

代码语言:javascript
复制
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

你知道问题出在哪里吗?这看起来像是rest服务返回的一个相当简单的json,但我还没能让它正常工作,任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2019-09-09 19:28:28

就在我写完问题后,我找到了答案,当然顶级类不应该是列表,只应该是类本身。但我有一个次要的问题,这三个映射器类是否可以挂载到一个类中,仅仅是一个表面上的问题?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57852880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档