0

I have a problem this in my android Json getting in postman is

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

When I call it from my android code with retrofit , I have error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 118 path $.data.whatsnew .My Format is wrong or Server data is wrong? This shows failure error .How can I fix ? My friend told that this is wrong in Json Format when adding attributes as collection.But it return as a String.

2
  • how are you parsing the response? you'll have to show the objects you're creating Commented Feb 8, 2019 at 5:13
  • I will edit this bro .Thanks for points Commented Feb 8, 2019 at 6:41

2 Answers 2

1

Expected BEGIN_ARRAY but was STRING at line 1 column 118 path

Above error means that you are accessing as String instead of Array Json.

Solution :- Try to access that variable using JsonArray.

Use these POJO in your retrofit...

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

}

Use the Result.class for your retrofit interface method signature.

Note:- It might be bcoz of your POJO class where you initialized a variable as String instead of List<YourObject>.

Try to covert your json into POJO using this LINK.

Sign up to request clarification or add additional context in comments.

2 Comments

I will try this,Thanks
your response may be changing .
0

Your Base Model would be something like this

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;

}

And please once ensure this model you has used like below in API Interface

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

I hope after above thing ensure your error will be resolved

@Ptut I got something missing in your posted Model Code once check it

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

You forgot to close List tag > of marketMobileBanner so you new code something like below

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

3 Comments

I will try this, Thanks
Above is just an example share you to ensure you have done something like that. I again see the error log and clearly indicate there second array "$.data.whatsnew" consider as a string so first, you can try with remove that later you need to do workaround near that.
I fixed it .I convert this JsonArray to ArrayList in server.And next fix is using JSonElement with changing custom.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.