com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列路径$

时间:2018-10-09 13:01:12

标签: java android gson

好吧,所以我读到了其他两个问题,但都出现了同样的错误,但是没有一个被回答为正常工作,并且看来我无法使其正常工作。 我正在连接到Google应用内结算并已设置了所有内容,但是,当我尝试拉出我的草图时(我现在有2个SKU),我收到了错误消息- 预期为BEGIN_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY,

我有一个SubscriptionActivity,Result(可序列化)和Details模型类(可序列化)。下面是代码,任何帮助都将非常有用,谢谢-

来自订阅活动:

Gson gson = new Gson();
try {
    Result result = gson.fromJson(skuDetailsList.toString(), Result.class);
    if (result != null) {
        for (Details d : result.getDetails()) {
            System.out.println(d.getProductId()
                + " \n " + d.getTitle() + " \n " + d.getDescription() + " \n "
                + d.getPrice());
        }
    }
} catch (NumberFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

从详细信息模型:

public class Details implements Serializable{

    @SerializedName("productId")
    @Expose
    private String productId;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("price")
    @Expose
    private String price;
    @SerializedName("price_amount_micros")
    @Expose
    private Integer priceAmountMicros;
    @SerializedName("price_currency_code")
    @Expose
    private String priceCurrencyCode;
    @SerializedName("subscriptionPeriod")
    @Expose
    private String subscriptionPeriod;
    @SerializedName("freeTrialPeriod")
    @Expose
    private String freeTrialPeriod;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("description")
    @Expose
    private String description;

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public Integer getPriceAmountMicros() {
        return priceAmountMicros;
    }

    public void setPriceAmountMicros(Integer priceAmountMicros) {
        this.priceAmountMicros = priceAmountMicros;
    }

    public String getPriceCurrencyCode() {
        return priceCurrencyCode;
    }

    public void setPriceCurrencyCode(String priceCurrencyCode) {
        this.priceCurrencyCode = priceCurrencyCode;
    }

    public String getSubscriptionPeriod() {
        return subscriptionPeriod;
    }

    public void setSubscriptionPeriod(String subscriptionPeriod) {
        this.subscriptionPeriod = subscriptionPeriod;
    }

    public String getFreeTrialPeriod() {
        return freeTrialPeriod;
    }

    public void setFreeTrialPeriod(String freeTrialPeriod) {
        this.freeTrialPeriod = freeTrialPeriod;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

来自“结果”活动:

public class Result implements Serializable{
    @SerializedName("SkuDetails")
    @Expose
    private ArrayList<Details> details = new ArrayList<Details>();
    /**
     *
     * @return The SkuDetails
     */
    public ArrayList<Details> getDetails() {
        return details;
    }
    /**
     *
     * @param details
     * The details
     */
    public void setDetails(ArrayList<Details> details) {
        this.details = details;
    }
}*

哦。我尝试解析的响应(skuDetailsList.toString())是:

[
    SkuDetails: {
        "productId": "basic_sub",
        "type": "subs",
        "price": "$0.99",
        "price_amount_micros": 990000,
        "price_currency_code": "USD",
        "subscriptionPeriod": "P1M",
        "freeTrialPeriod": "P4W2D",
        "title": "Basic Subscription Service (DadBod Recipes)",
        "description": "Basic Subscription Service for DadBodRecipes"
    },
    SkuDetails: {
        "productId": "enterprise_sub",
        "type": "subs",
        "price": "$2.99",
        "price_amount_micros": 2990000,
        "price_currency_code": "USD",
        "subscriptionPeriod": "P1M",
        "freeTrialPeriod": "P4W2D",
        "title": "Enterprise Subscription Service (DadBod Recipes)",
        "description": "Enterprise Subscription Service for DadBodRecipes"
    }
]

2 个答案:

答案 0 :(得分:0)

问题是因为,您得到的结果是<< strong>键值>对(不是JSON对象/数组,但与此类似)。< / p>

因此,您需要先将其设置为JSONObject,然后使用Gson对其进行解析,如下所示:

Map<String, String> params = skuDetailsList;
JSONObject object = new JSONObject(params);
Result result = gson.fromJson(object.toString(), Result.class);

这样做,希望对您有所帮助!

答案 1 :(得分:0)

  

您正在尝试解析json

     

[

     

     

{

当您看到[它代表一个列表

看到{代表一个对象时。

我很确定您在构建包装器类时就知道,但是包装器类也是对象,而不是数组。

因此,您的选择是让包装器类扩展ArrayList或某种形式的List。

告诉您的Json转换器基数是一个数组,并且您希望列表中的第一个对象是您类型的对象。