简体   繁体   中英

GSON throwing error when trying to pares a simple rest api response: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

I'm trying to parse a JSON like this

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

generated for http://www.jsonschema2pojo.org I got his Java clases

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 class

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

}

Object class

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

}

But when trying to deserialise with this func

Type localVarReturnType = new TypeToken<List<InstanceCreatorForAwsMarketplace>>(){}.getType();
ApiResponse<List<InstanceCreatorForAwsMarketplace>> responseFromApiCleint = apiClient.execute(newCall, localVarReturnType);

I get the following error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Any idea what might be the issue? This looks like a fairly simple json that the rest service returns, but I have not been able to get it work, any help would be greatly appreciated.

Right after I had written the question, I found out the answer, of course the top level class should not be a list, just the class it self. But I have a secondary question, can the three mapper classes be mounted into one class, just a cosmetic issue?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM