预期为BEGIN_ARRAY,但在此代码中为STRING

时间:2019-02-08 05:10:46

标签: android json parsing gson retrofit

我的Android设备有问题 杰森上邮递员是

{
    "syskey": "base64:5i/Sy3ujzpOhqTowu838H2gx2Uk4+LR4tLkK5eOZgfk=",
    "createDate": "",
    "updateDate": "",
    "data": {
        "market_mobile_banner": [
            {
                "id": 1,
                "banner_txt": "banner1",
                "banner_price": "12.99",
                "banner_file": "https://webqr.saltcloudserver.com/storage/banner/PumdmssQTRKpAfyD7a6ai5NTCJJd7fFpOmCqKmPH.png"
            },
            {
                "id": 2,
                "banner_txt": "banner2",
                "banner_price": "33.95",
                "banner_file": "https://webqr.saltcloudserver.com/storage/banner/X9sxhmREMRbyYV1poo2UnHUJKF9Bt6eQqcBL2LVn.jpeg"
            },
            {
                "id": 3,
                "banner_txt": "Banner3",
                "banner_price": "5.66",
                "banner_file": "https://webqr.saltcloudserver.com/storage/banner/4dGmwqiFB0V4qGfX1ZkEVFrILZUekvE0pPsjfCID.jpeg"
            }
        ],
        "whatsnew": [
            {
                "id": 1,
                "new_txt": "new1",
                "new_file": "https://webqr.saltcloudserver.com/storage/new/Z1DB604SqG43tc3XJbJXxvqwES3H4BofMimnMVqP.jpeg"
            },
            {
                "id": 2,
                "new_txt": "new2",
                "new_file": "https://webqr.saltcloudserver.com/storage/new/FW39O5VLPvF4aBXd1UQwWWP3m6NLwx7IMSnYvHXk.jpeg"
            },
            {
                "id": 3,
                "new_txt": "new3",
                "new_file": "https://webqr.saltcloudserver.com/storage/new/Srh5nCQekGLH9IPbmAkqwFV6pMou7yCXbkpWGp2l.jpeg"
            }
        ]
    }
}

GetHomeResponse.kt

data class GetHomeResponse(
    val syskey: String? = null,
    val createDate: String? = null,
    val updateDate: String? = null,
    val data: HomeData? = null)

HomeData.kt

data class HomeData(
     @SerializedName("market_mobile_banner")
     val marketMobileBanner: List<MarketMobileBannerItem?=null,
    val whatsnew: List<WhatsnewItem>?=null)

MarketMobileBannerItem.kt

data class MarketMobileBannerItem(
     val id: Int? = null,
     @SerializedName("banner_txt")
     val bannerTxt: String? = null,
     @SerializedName("banner_price")
     val bannerPrice: String? = null,
     @SerializedName("banner_file")
     val bannerFile: String? = null)

WhatsNewItem.kt

data class WhatsnewItem(
     val id: Int? = null,
     @SerializedName("new_txt")
     @Expose
     val newTxt: String? = null,
     @SerializedName("new_file")
     @Expose
     val newFile: String? = null)

WebQr.kt

interface WebQrApi {
  @GET("home/{outLetId}")
  fun getHomeData(@Path("outLetId") 
  outLetId:Int):Observable<GetHomeResponse>}

当我从我的Android代码进行翻新调用时,出现错误 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 118 path $.data.whatsnew .我的格式错误或服务器数据错误? 这显示失败错误。如何解决?  我的朋友告诉我,在将属性添加为集合时,这在Json格式中是错误的。但是它以字符串形式返回。

2 个答案:

答案 0 :(得分:1)

  

预期为BEGIN_ARRAY,但在第1行第118列的路径上为STRING

以上错误表示您正在以String而不是Array Json的身份访问。

解决方案:-尝试使用JsonArray访问该变量。

在改造中使用这些POJO ...

  

Data.class

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

public class Data {

@SerializedName("market_mobile_banner")
@Expose
private List<MarketMobileBanner> marketMobileBanner = null;
@SerializedName("whatsnew")
@Expose
private List<Whatsnew> whatsnew = null;

public List<MarketMobileBanner> getMarketMobileBanner() {
return marketMobileBanner;
}

public void setMarketMobileBanner(List<MarketMobileBanner> marketMobileBanner) {
this.marketMobileBanner = marketMobileBanner;
}

public List<Whatsnew> getWhatsnew() {
return whatsnew;
}

public void setWhatsnew(List<Whatsnew> whatsnew) {
this.whatsnew = whatsnew;
}

}
  

MarketMobileBanner.class

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

public class MarketMobileBanner {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("banner_txt")
@Expose
private String bannerTxt;
@SerializedName("banner_price")
@Expose
private String bannerPrice;
@SerializedName("banner_file")
@Expose
private String bannerFile;

public Integer getId() {
return id;
}

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

public String getBannerTxt() {
return bannerTxt;
}

public void setBannerTxt(String bannerTxt) {
this.bannerTxt = bannerTxt;
}

public String getBannerPrice() {
return bannerPrice;
}

public void setBannerPrice(String bannerPrice) {
this.bannerPrice = bannerPrice;
}

public String getBannerFile() {
return bannerFile;
}

public void setBannerFile(String bannerFile) {
this.bannerFile = bannerFile;
}

}
  

Result.class

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

public class Result {

@SerializedName("syskey")
@Expose
private String syskey;
@SerializedName("createDate")
@Expose
private String createDate;
@SerializedName("updateDate")
@Expose
private String updateDate;
@SerializedName("data")
@Expose
private Data data;

public String getSyskey() {
return syskey;
}

public void setSyskey(String syskey) {
this.syskey = syskey;
}

public String getCreateDate() {
return createDate;
}

public void setCreateDate(String createDate) {
this.createDate = createDate;
}

public String getUpdateDate() {
return updateDate;
}

public void setUpdateDate(String updateDate) {
this.updateDate = updateDate;
}

public Data getData() {
return data;
}

public void setData(Data data) {
this.data = data;
}

}
  

Whatsnew.class

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

public class Whatsnew {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("new_txt")
@Expose
private String newTxt;
@SerializedName("new_file")
@Expose
private String newFile;

public Integer getId() {
return id;
}

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

public String getNewTxt() {
return newTxt;
}

public void setNewTxt(String newTxt) {
this.newTxt = newTxt;
}

public String getNewFile() {
return newFile;
}

public void setNewFile(String newFile) {
this.newFile = newFile;
}

}

使用Result.class进行改造接口方法签名。

注意:-可能是POJO类的bcoz,其中您将变量初始化为String而不是List<YourObject>

尝试使用此LINK将json秘密隐藏到POJO中。

答案 1 :(得分:0)

您的基本模型将是这样

public class MyPojo {

    @SerializedName("syskey")
    @Expose
    public String syskey;
    @SerializedName("createDate")
    @Expose
    public String createDate;
    @SerializedName("updateDate")
    @Expose
    public String updateDate;
    @SerializedName("data")
    @Expose
    public Data data;

}

并且请确保一旦在API接口中使用了该模型,如下所示

@FormUrlEncoded
@POST("api_url")
Call<MyPojo> callGetData(@FieldMap Map<String, String> method);

希望以上所述确保您的错误得到解决

@Ptut一旦检查,我在您发布的模型代码中缺少了一些东西

data class HomeData(
@SerializedName("market_mobile_banner")
val marketMobileBanner: List<MarketMobileBannerItem?=null,
val whatsnew: List<WhatsnewItem>?=null)

您忘了关闭marketMobileBanner的列表标签> ,因此您在下面添加了新的代码

data class HomeData(
@SerializedName("market_mobile_banner")
val marketMobileBanner: List<MarketMobileBannerItem>?=null,
val whatsnew: List<WhatsnewItem>?=null)