Java GSON在尝试削减简单rest api响应时抛出错误:应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象$
我正试图像这样解析一个JSONJava GSON在尝试削减简单rest api响应时抛出错误:应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象$,java,gson,Java,Gson,我正试图像这样解析一个JSON { "meta": { "limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1 }, "objects": [ { "customer_id": "some-customer-id", "id": 5,
{
"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"
}
]
}
为我得到他的Java类生成
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;
}
}
对象类
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;
}
}
但是当试图用这个函数反序列化时
Type localVarReturnType = new TypeToken<List<InstanceCreatorForAwsMarketplace>>(){}.getType();
ApiResponse<List<InstanceCreatorForAwsMarketplace>> responseFromApiCleint = apiClient.execute(newCall, localVarReturnType);
你知道可能是什么问题吗?这看起来像rest服务返回的一个相当简单的json,但我无法让它工作,任何帮助都将不胜感激。在我写完问题后,我马上找到了答案,当然顶级类不应该是列表,而应该是它自己的类。但我还有第二个问题,三个mapper类是否可以装入一个类,只是一个表面问题
Type localVarReturnType = new TypeToken<List<InstanceCreatorForAwsMarketplace>>(){}.getType();
ApiResponse<List<InstanceCreatorForAwsMarketplace>> responseFromApiCleint = apiClient.execute(newCall, localVarReturnType);
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $