Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 53 path $.data

expected begin_object but was string at line 1 column 1 path $ retrofit
expected begin_array but was begin_object android
java lang illegalstateexception expected begin_array but was begin_object at line 1 column 15 path
java.lang.illegalstateexception expected begin_object but was string at line 1 casper
expected begin_array but was boolean at line
expected begin_object but was number
expected begin_object but was begin_array at line 1 column 66 path data
com.squareup.moshi.jsondataexception: expected begin_object but was begin_array at path $

I'm trying to show a data from JSON to RecyclerView using retrofit, but i keep getting this error somehow, and i don't know what did i do wrong.

Error Log

E/response-failure-coy: retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@d07e1a7 2018-12-02 20:06:05.182 19205-19205/com.qiscus.qismo.chat

E/Error Kims: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 53 path $.data

ApiInterface.java

@FormUrlEncoded
 @POST("order-management-data-by-salesperson")
 Call<OrderListModel> getOrderList(@Field("sales_people_id") String salesPeopleId);

JSON Response

{
    "code": 100,
    "status": "Showing Data Success",
    "data": {
        "current_page": 1,
        "data": [
            {
                "mokita_transact_id": 2,
                "diab_transact_id": "2",
                "order_status": "cancel",
                "leasing_status": "approved",
                "id_card_num": "1",
                "hot_leads_id": 2,
                "qty": 1,
                "retail_dealer_id": 1,
                "rd_code": "1",
                "customer_name": "arya jelek",
                "birthdate": "2018-11-27",
                "gender": 1,
                "address": "Jalan",
                "kelurahan_id": 1,
                "kelurahan": "1",
                "kecamatan_id": 1,
                "kecamatan": "1",
                "customer_number": "082283494984",
                "kota_id": 1,
                "kota": 1,
                "postal_code": 1,
                "payment_option_id": 1,
                "payment_type": "1",
                "datetime": "123",
                "product_variant_id": 9,
                "model_code": "2",
                "color_code": "1",
                "credit_provider_id": 1,
                "dp": 11000,
                "credit_term_id": 1,
                "credit_term": 123,
                "monthly_installment": 12,
                "sales_people_id": "14",
                "spk_number": 1,
                "is_indent": 1,
                "hot_leads": {
                    "id": 2,
                    "sales_person_id": 14,
                    "sales_person": {
                        "id": 14,
                        "id_source_MD": 1,
                        "id_source_RD": 1,
                        "role": "SP",
                        "username": "ABE.SP",
                        "email": "abe-sp@bcg.com",
                        "sdk_email": "9rjYA_abe-sp@bcg.com",
                        "phone_number": "081288800908",
                        "role_app": "SP"
                    }
                },
                "payment_detail": {
                    "id": 1,
                    "name": "Cash",
                    "description": "Cash"
                },
                "product_variant_motor": {
                    "id": 9,
                    "ahm_sku_id": 9,
                    "ahm_sku": "Y1G02N02S1 A/T",
                    "type": "Beat Pop ESP CW",
                    "bypass_color": 0,
                    "product_category_id": 1,
                    "product_category": "MATIC",
                    "product_unit_id": 3,
                    "product_unit": "Beat",
                    "md_sku_id": "H9",
                    "main_dealer_type": "Beat Pop ESP CW",
                    "otr_price": 5000009,
                    "city_id": 2,
                    "main_dealer_id": 1,
                    "lowest_dp_amount": 6000000,
                    "lowest_monthly_installment": 1000000
                }
            }
        ],
        "first_page_url": "http://23.101.25.168/api/sales-app/order-management-data-by-salesperson?page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "http://23.101.25.168/api/sales-app/order-management-data-by-salesperson?page=1",
        "next_page_url": null,
        "path": "http://23.101.25.168/api/sales-app/order-management-data-by-salesperson",
        "per_page": 20,
        "prev_page_url": null,
        "to": 1,
        "total": 1
    }
}

OrderListModel.java

public class OrderListModel {
    @SerializedName("current_page")
    @Expose
    private Integer currentPage;
    @SerializedName("data")
    @Expose
    private List<OrderListResponse> data = null;
    @SerializedName("first_page_url")
    @Expose
    private String firstPageUrl;
    @SerializedName("from")
    @Expose
    private Integer from;
    @SerializedName("last_page")
    @Expose
    private Integer lastPage;
    @SerializedName("last_page_url")
    @Expose
    private String lastPageUrl;
    @SerializedName("next_page_url")
    @Expose
    private Object nextPageUrl;
    @SerializedName("path")
    @Expose
    private String path;
    @SerializedName("per_page")
    @Expose
    private Integer perPage;
    @SerializedName("prev_page_url")
    @Expose
    private Object prevPageUrl;
    @SerializedName("to")
    @Expose
    private Integer to;
    @SerializedName("total")
    @Expose
    private Integer total;

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public List<OrderListResponse> getData() {
        return data;
    }

    public void setData(List<OrderListResponse> data) {
        this.data = data;
    }

    public String getFirstPageUrl() {
        return firstPageUrl;
    }

    public void setFirstPageUrl(String firstPageUrl) {
        this.firstPageUrl = firstPageUrl;
    }

    public Integer getFrom() {
        return from;
    }

    public void setFrom(Integer from) {
        this.from = from;
    }

    public Integer getLastPage() {
        return lastPage;
    }

    public void setLastPage(Integer lastPage) {
        this.lastPage = lastPage;
    }

    public String getLastPageUrl() {
        return lastPageUrl;
    }

    public void setLastPageUrl(String lastPageUrl) {
        this.lastPageUrl = lastPageUrl;
    }

    public Object getNextPageUrl() {
        return nextPageUrl;
    }

    public void setNextPageUrl(Object nextPageUrl) {
        this.nextPageUrl = nextPageUrl;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Integer getPerPage() {
        return perPage;
    }

    public void setPerPage(Integer perPage) {
        this.perPage = perPage;
    }

    public Object getPrevPageUrl() {
        return prevPageUrl;
    }

    public void setPrevPageUrl(Object prevPageUrl) {
        this.prevPageUrl = prevPageUrl;
    }

    public Integer getTo() {
        return to;
    }

    public void setTo(Integer to) {
        this.to = to;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }
}

OrderListResponse.java

public class OrderListResponse {
    @SerializedName("mokita_transact_id")
    @Expose
    private Integer mokitaTransactId;
    @SerializedName("diab_transact_id")
    @Expose
    private String diabTransactId;
    @SerializedName("order_status")
    @Expose
    private String orderStatus;
    @SerializedName("leasing_status")
    @Expose
    private String leasingStatus;
    @SerializedName("id_card_num")
    @Expose
    private String idCardNum;
    @SerializedName("hot_leads_id")
    @Expose
    private Integer hotLeadsId;
    @SerializedName("qty")
    @Expose
    private Integer qty;
    @SerializedName("retail_dealer_id")
    @Expose
    private Integer retailDealerId;
    @SerializedName("rd_code")
    @Expose
    private String rdCode;
    @SerializedName("customer_name")
    @Expose
    private String customerName;
    @SerializedName("birthdate")
    @Expose
    private String birthdate;
    @SerializedName("gender")
    @Expose
    private Integer gender;
    @SerializedName("address")
    @Expose
    private String address;
    @SerializedName("kelurahan_id")
    @Expose
    private Integer kelurahanId;
    @SerializedName("kelurahan")
    @Expose
    private String kelurahan;
    @SerializedName("kecamatan_id")
    @Expose
    private Integer kecamatanId;
    @SerializedName("kecamatan")
    @Expose
    private String kecamatan;
    @SerializedName("customer_number")
    @Expose
    private String customerNumber;
    @SerializedName("kota_id")
    @Expose
    private Integer kotaId;
    @SerializedName("kota")
    @Expose
    private Integer kota;
    @SerializedName("postal_code")
    @Expose
    private Integer postalCode;
    @SerializedName("payment_option_id")
    @Expose
    private Integer paymentOptionId;
    @SerializedName("payment_type")
    @Expose
    private String paymentType;
    @SerializedName("datetime")
    @Expose
    private String datetime;
    @SerializedName("product_variant_id")
    @Expose
    private Integer productVariantId;
    @SerializedName("model_code")
    @Expose
    private String modelCode;
    @SerializedName("color_code")
    @Expose
    private String colorCode;
    @SerializedName("credit_provider_id")
    @Expose
    private Integer creditProviderId;
    @SerializedName("dp")
    @Expose
    private Integer dp;
    @SerializedName("credit_term_id")
    @Expose
    private Integer creditTermId;
    @SerializedName("credit_term")
    @Expose
    private Integer creditTerm;
    @SerializedName("monthly_installment")
    @Expose
    private Integer monthlyInstallment;
    @SerializedName("sales_people_id")
    @Expose
    private String salesPeopleId;
    @SerializedName("spk_number")
    @Expose
    private Integer spkNumber;
    @SerializedName("is_indent")
    @Expose
    private Integer isIndent;
    @SerializedName("hot_leads")
    @Expose
    private HotLeadsResponse hotLeads;
    @SerializedName("payment_detail")
    @Expose
    private PaymentDetailResponse paymentDetail;
    @SerializedName("product_variant_motor")
    @Expose
    private ProductVariantResponse productVariantMotor;

    public Integer getMokitaTransactId() {
        return mokitaTransactId;
    }

    public void setMokitaTransactId(Integer mokitaTransactId) {
        this.mokitaTransactId = mokitaTransactId;
    }

    public String getDiabTransactId() {
        return diabTransactId;
    }

    public void setDiabTransactId(String diabTransactId) {
        this.diabTransactId = diabTransactId;
    }

    public String getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    public String getLeasingStatus() {
        return leasingStatus;
    }

    public void setLeasingStatus(String leasingStatus) {
        this.leasingStatus = leasingStatus;
    }

    public String getIdCardNum() {
        return idCardNum;
    }

    public void setIdCardNum(String idCardNum) {
        this.idCardNum = idCardNum;
    }

    public Integer getHotLeadsId() {
        return hotLeadsId;
    }

    public void setHotLeadsId(Integer hotLeadsId) {
        this.hotLeadsId = hotLeadsId;
    }

    public Integer getQty() {
        return qty;
    }

    public void setQty(Integer qty) {
        this.qty = qty;
    }

    public Integer getRetailDealerId() {
        return retailDealerId;
    }

    public void setRetailDealerId(Integer retailDealerId) {
        this.retailDealerId = retailDealerId;
    }

    public String getRdCode() {
        return rdCode;
    }

    public void setRdCode(String rdCode) {
        this.rdCode = rdCode;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getKelurahanId() {
        return kelurahanId;
    }

    public void setKelurahanId(Integer kelurahanId) {
        this.kelurahanId = kelurahanId;
    }

    public String getKelurahan() {
        return kelurahan;
    }

    public void setKelurahan(String kelurahan) {
        this.kelurahan = kelurahan;
    }

    public Integer getKecamatanId() {
        return kecamatanId;
    }

    public void setKecamatanId(Integer kecamatanId) {
        this.kecamatanId = kecamatanId;
    }

    public String getKecamatan() {
        return kecamatan;
    }

    public void setKecamatan(String kecamatan) {
        this.kecamatan = kecamatan;
    }

    public String getCustomerNumber() {
        return customerNumber;
    }

    public void setCustomerNumber(String customerNumber) {
        this.customerNumber = customerNumber;
    }

    public Integer getKotaId() {
        return kotaId;
    }

    public void setKotaId(Integer kotaId) {
        this.kotaId = kotaId;
    }

    public Integer getKota() {
        return kota;
    }

    public void setKota(Integer kota) {
        this.kota = kota;
    }

    public Integer getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(Integer postalCode) {
        this.postalCode = postalCode;
    }

    public Integer getPaymentOptionId() {
        return paymentOptionId;
    }

    public void setPaymentOptionId(Integer paymentOptionId) {
        this.paymentOptionId = paymentOptionId;
    }

    public String getPaymentType() {
        return paymentType;
    }

    public void setPaymentType(String paymentType) {
        this.paymentType = paymentType;
    }

    public String getDatetime() {
        return datetime;
    }

    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }

    public Integer getProductVariantId() {
        return productVariantId;
    }

    public void setProductVariantId(Integer productVariantId) {
        this.productVariantId = productVariantId;
    }

    public String getModelCode() {
        return modelCode;
    }

    public void setModelCode(String modelCode) {
        this.modelCode = modelCode;
    }

    public String getColorCode() {
        return colorCode;
    }

    public void setColorCode(String colorCode) {
        this.colorCode = colorCode;
    }

    public Integer getCreditProviderId() {
        return creditProviderId;
    }

    public void setCreditProviderId(Integer creditProviderId) {
        this.creditProviderId = creditProviderId;
    }

    public Integer getDp() {
        return dp;
    }

    public void setDp(Integer dp) {
        this.dp = dp;
    }

    public Integer getCreditTermId() {
        return creditTermId;
    }

    public void setCreditTermId(Integer creditTermId) {
        this.creditTermId = creditTermId;
    }

    public Integer getCreditTerm() {
        return creditTerm;
    }

    public void setCreditTerm(Integer creditTerm) {
        this.creditTerm = creditTerm;
    }

    public Integer getMonthlyInstallment() {
        return monthlyInstallment;
    }

    public void setMonthlyInstallment(Integer monthlyInstallment) {
        this.monthlyInstallment = monthlyInstallment;
    }

    public String getSalesPeopleId() {
        return salesPeopleId;
    }

    public void setSalesPeopleId(String salesPeopleId) {
        this.salesPeopleId = salesPeopleId;
    }

    public Integer getSpkNumber() {
        return spkNumber;
    }

    public void setSpkNumber(Integer spkNumber) {
        this.spkNumber = spkNumber;
    }

    public Integer getIsIndent() {
        return isIndent;
    }

    public void setIsIndent(Integer isIndent) {
        this.isIndent = isIndent;
    }

    public HotLeadsResponse getHotLeads() {
        return hotLeads;
    }

    public void setHotLeads(HotLeadsResponse hotLeads) {
        this.hotLeads = hotLeads;
    }

    public PaymentDetailResponse getPaymentDetail() {
        return paymentDetail;
    }

    public void setPaymentDetail(PaymentDetailResponse paymentDetail) {
        this.paymentDetail = paymentDetail;
    }

    public ProductVariantResponse getProductVariantMotor() {
        return productVariantMotor;
    }

    public void setProductVariantMotor(ProductVariantResponse productVariantMotor) {
        this.productVariantMotor = productVariantMotor;
    }

}

OrderListingAdapter.java

public class OrderListingAdapter extends RecyclerView.Adapter<OrderListingAdapter.OrderListingViewHolder> {

    private List<OrderListResponse> customerName;

    public OrderListingAdapter(List<OrderListResponse> customerName) {
        this.customerName = customerName;
    }


    @Override
    public OrderListingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View layoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_order_lists, null);

        OrderListingViewHolder recyclerViewHolder = new OrderListingViewHolder(layoutView);

        return recyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(OrderListingViewHolder holder, int position) {
        holder.txtCustomerName.setText(customerName.get(position).getCustomerName());
        holder.txtMotorName.setText(customerName.get(position).getProductVariantMotor().getType());
    }

    @Override
    public int getItemCount() {
        return customerName.size();
    }

    class OrderListingViewHolder extends RecyclerView.ViewHolder {

        TextView txtCustomerName, txtMotorName;

        OrderListingViewHolder(View itemView) {
            super(itemView);
            txtCustomerName = (TextView) itemView.findViewById(R.id.textViewCustomerName);
            txtMotorName = (TextView) itemView.findViewById(R.id.textViewMotor);
        }
    }

}

OrderListingFragment.java

public class OrderListingFragment extends Fragment {
    View v;
    private OrderListingAdapter adapter;
    private RecyclerView recyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_order_listing, container, false);

//        JsonObject jsonObject = new JsonObject();
//        jsonObject.addProperty("sales_people_id",14);
        ApiInterface service = ApiClient.getClient().create(ApiInterface.class);
        Call<OrderListModel> call = service.getOrderList("14");

        call.enqueue(new Callback<OrderListModel>() {
            @Override
            public void onResponse(Call <OrderListModel> call, Response<OrderListModel> response) {
                try {
                    if (response.code() == 200) {
                        Log.e("RECYCLER",""+response.body());
                        generateEmployeeList(response.body().getData());
                    } else {
                        Log.e("ERROR CODE", "" + response.code());
                        Log.e("Error Message", "" + response.errorBody());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<OrderListModel> call, Throwable t) {
                Log.e("response-failure-coy", call.toString());
                t.printStackTrace();
                Log.e("Error Kims",""+t.getMessage());
            }
        });
        return v;
    }
    private void generateEmployeeList(List<OrderListResponse> empDataList) {
        recyclerView = (RecyclerView) v.findViewById(R.id.recyclerOrderList);

        adapter = new OrderListingAdapter(empDataList);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(v.getContext());

        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setAdapter(adapter);
    }
}

How could i solve this error ? Thanks in advance.

Your OrderListModel class doesn't match the JSON response you get. You expect properties like current_page, data, first_page_url on the outer most object. Those properties are part of the response but they are nested within another object.

So you need an additional class:

public class OrderResponse {
    @SerializedName("code")
    @Expose
    private Integer code;

    @SerializedName("status")
    @Expose
    private String status;

    @SerializedName("data")
    @Expose
    private OrderListModel data;

    ...

The interface then becomes:

@FormUrlEncoded
@POST("order-management-data-by-salesperson")
Call<OrderResponse> getOrderList(@Field("sales_people_id") String salesPeopleId);

And further changes are needed for the rest of your code...

retrofit expected begin_array but was begin_object at line 1 column , As you Can see the Given REST API url returning an array of Object , that is ArrayList but in your retrofit api service the return type is Only Cliente. So change​  Your OrderListModel class doesn't match the JSON response you get. You expect properties like current_page, data, first_page_url on the outer most object. Those properties are part of the response but they are nested within another object.

In OrderListModel class you have private List<OrderListResponse> data = null; and in JSON it is a single element, not an array of elements, so this error is thrown.

Change this to a single OrderListResponse not a List of those, and this error will disappear, but another will appear. Generally your Java model is not fitting the JSON.

Try this: link

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was , lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ #1462. Closed. When I am trying to parse a JSON object into a list of object, I am getting an exception like, com. line 1 column 2 Is there any way to fix it? 34064/gson-throwing-expected-begin-object-but-was-begin-array

The response you getting retrofit call in JSON format is expecting to store that result in an array But you are storing the result in object. Change the type to array and try storing in it. Just check the format of json and then analyze whether it can be stored in array and object.

Expected BEGIN_ARRAY but was STRING at line 1 column 1 path , Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ #966 Expected BEGIN_OBJECT but was STRING at line 12 column 11 path so your API need to return response using $data: Class::find()->first(); com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.Gson.fromJson

[#JRASERVER-67578], JsonSyntaxException: Expected BEGIN_ARRAY but was BEGIN_OBJECT cards for board with ID {1}. at com.atlassian.jira.plugins.importer.trello.fetch. ServiceTCCLInterceptor.invoke(ServiceTCCLInterceptor.java:53) at IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 at  Hey, @Vickey, Could you please post your object class, what you are using?

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column , IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 53 path $.data. ApiInterface.java @FormUrlEncoded  java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 #53

Retrofit, Your problem is Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ so The "result" node in the json data is an object not an array. How to fix "expected begin_array but was begin_object" in Retrofit ? Before we begin, keep this in mind begin_array means the json response is an array which will look something like this [{},{},..]