我正在尝试像这样解析JSON
{
"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类
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类
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;
}
}对象类
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);我得到以下错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $你知道问题出在哪里吗?这看起来像是rest服务返回的一个相当简单的json,但我还没能让它正常工作,任何帮助都将不胜感激。
发布于 2019-09-09 19:28:28
就在我写完问题后,我找到了答案,当然顶级类不应该是列表,只应该是类本身。但我有一个次要的问题,这三个映射器类是否可以挂载到一个类中,仅仅是一个表面上的问题?
https://stackoverflow.com/questions/57852880
复制相似问题