Java – Expected BEGIN_OBJECT but column 2 on line 1, column BEGIN_ARRAY (minor edit)

Expected BEGIN_OBJECT but column 2 on line 1, column BEGIN_ARRAY (minor edit)… here is a solution to the problem.

Expected BEGIN_OBJECT but column 2 on line 1, column BEGIN_ARRAY (minor edit)

What I have here is a web service that gives me the following JSON code:

[
  {
    "_OrderDetails": [
      {
         "ProductName": "FUCHS SUPER GT SAE 10W30 6X5 / FP10100010102",
        "TotalAfterDiscount_Lc": "7500",
        "MeasureUnitName": "كرتونة",
        "TotalPrice_Lc": "7500",
        "PricePerUnit_Lc": "75",
        "Quantity": "100"
      }
    ],
    "Id": "274",
    "OrderDate": "4/10/2014 12:00:00 AM",
    "Number": "16",
    "CustomerName": "الأسد",
    "Note": ""
  }
]

I’ve made a java class (entity) with getters and setters for all data:

package com.example.webservicetest;

import java.util.List;

public class Item {

private String OrderDate;
private String Number;
private String Note;
private String CustomerName;
private String Id;
private List<_OrderDetails> orderDetails;

public String getOrderDate() {
    return OrderDate;
}
public void setOrderDate(String orderDate) {
    OrderDate = orderDate;
}
public String getNumber() {
    return Number;
}
public void setNumber(String number) {
    Number = number;
}
public String getNote() {
    return Note;
}
public void setNote(String note) {
    Note = note;
}

public String getId() {
    return Id;
}
public void setId(String id) {
    Id = id;
}
public String getCustomerName() {
    return CustomerName;
}
public void setCustomerName(String customerName) {
    CustomerName = customerName;
}
public List<_OrderDetails> getOrderDetails() {
    return orderDetails;
}
public void setOrderDetails(List<_OrderDetails> orderDetails) {
    this.orderDetails = orderDetails;
}
public class _OrderDetails{
    private String OrderId;
    private String OrderDate;
    private String Number;
    private String Note;
    private String ProductName;
    private String TotalAfterDiscount_Lc;
    private String MeasureUnitName;
    private String TotalPrice_Lc;
    private String PricePerUnit_Lc;
    private String Quantity;
    public String getOrderId() {
        return OrderId;
    }
    public void setOrderId(String orderId) {
        OrderId = orderId;
    }
    public String getOrderDate() {
        return OrderDate;
    }
    public void setOrderDate(String orderDate) {
        OrderDate = orderDate;
    }
    public String getNumber() {
        return Number;
    }
    public void setNumber(String number) {
        Number = number;
    }
    public String getNote() {
        return Note;
    }
    public void setNote(String note) {
        Note = note;
    }
    public String getProductName() {
        return ProductName;
    }
    public void setProductName(String productName) {
        ProductName = productName;
    }
    public String getTotalAfterDiscount_Lc() {
        return TotalAfterDiscount_Lc;
    }
    public void setTotalAfterDiscount_Lc(String totalAfterDiscount_Lc) {
        TotalAfterDiscount_Lc = totalAfterDiscount_Lc;
    }
    public String getMeasureUnitName() {
        return MeasureUnitName;
    }
    public void setMeasureUnitName(String measureUnitName) {
        MeasureUnitName = measureUnitName;
    }
    public String getTotalPrice_Lc() {
        return TotalPrice_Lc;
    }
    public void setTotalPrice_Lc(String totalPrice_Lc) {
        TotalPrice_Lc = totalPrice_Lc;
    }
    public String getPricePerUnit_Lc() {
        return PricePerUnit_Lc;
    }
    public void setPricePerUnit_Lc(String pricePerUnit_Lc) {
        PricePerUnit_Lc = pricePerUnit_Lc;
    }
    public String getQuantity() {
        return Quantity;
    }
    public void setQuantity(String quantity) {
        Quantity = quantity;
    }

}

}

In the main activity, I get data like this:

Item[] placelist;
placelist = gson.fromJson(responseJSON, Item[].class);
Item item = gson.fromJson(responseJSON, Item.class);

But I get the following exception in logcat:
Should be BEGIN_OBJECT but column 2 in line 1, column 2 is BEGIN_ARRAY

What am I doing wrong???

Solution

Looking at your code, the correct way to get the item object is

Item[] placelist = gson.fromJson(responseJSON, Item[].class);    

Because your JSON is a list of item objects[(BEGIN_ARRAY).

Item item = gson.fromJson(responseJSON, Item.class);    

An exception is thrown because Gson expects a single item object { (BEGIN_OBJECT) but it is an array.

You cannot deserialize the same JSON in both an array and an object, deserialize it to an array if your JSON is an

array, and deserialize it to an object if your JSON is an object, but you cannot deserialize it both ways.

Related Problems and Solutions