what I am having here is a web service that gives me the following JSON code :
[
{
\"_OrderDetails\": [
{
\"ProductName\": \"FUCHS SUPER
Your JSON output is a list.
// This parses your JSON
Item[] items = new Gson().fromJson(responseJson, Item[].class);
After this you'll have array of items
Here is how you should define your class:
public class Item implements Serializable {
@SerializedName("_OrderDetails")
private OrderDetails[] mOrderDetails;
@SerializedName("Id")
private String mId;
@SerializedName("OrderData")
private String mOrderDate;
@SerializedName("Number")
private String mNumber;
@SerializedName("CustomerName")
private String mCustomerName;
@SerializedName("Note")
private String mNote;
// Add setters and getters
public static class OrderDetails implements Serializable {
@SerializedName("ProductName")
private String mProductName;
@SerializedName("TotalAfterDiscount_Lc")
private String mTotalAfterDiscount;
@SerializedName("MeasureUnitName")
private String mMeasureUnitName;
@SerializedName("TotalPrice_Lc"
private String mTotalPrice;
@SerializedName("PricePerUnit_Lc")
private String mPricePerUnit;
@SerializedName("Quantity")
private String mQuantity;
// Add setters and getters
}
}
EDIT. With the code snippet below you can deserialize the JSON into Item
object.
Gson gson = new Gson();
Item item = gson.fromJson(json, Item.class);
Looking at your code, the right way to get your item
objects is
Item[] placelist = gson.fromJson(responseJSON, Item[].class);
because your JSON is an item
object list [
(BEGIN_ARRAY)
Item item = gson.fromJson(responseJSON, Item.class);
throws an exception because Gson is expecting an single item object {
(BEGIN_OBJECT) but is an array.
You can not deserialize the same JSON in two ways, as array and as an object, if your JSON is an array, deserialize it as an array, if you JSON is an object deserialize it as an object but you can not deserialize in both ways.
Actually, because you have an array of objects not an object. And that is why the second line works and the third line doesn't. So if you want to parse this json as an Item[] placelist; you don't need to change anything. If you want to parse it as an Object you should remove brackets like this:
{
"_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": ""
}
Or parse it like an array and retrieve the first element.
UPDATE
Item[] placelist = gson.fromJson(responseJSON, Item[].class);
Works OK and you've got an array of Items. But you have a problem with the name of OrderDetails. In json it is a "_OrderDetails" in your code it is "orderDetails". You can add annotation on your field:
@SerializedName("_OrderDetails")
private List<_OrderDetails> orderDetails;
Full code to test:
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String responseJSON = "[\n" +
" {\n" +
" \"_OrderDetails\": [\n" +
" {\n" +
" \"ProductName\": \"FUCHS SUPER GT SAE 10W30 6X5 / FP10100010102\",\n" +
" \"TotalAfterDiscount_Lc\": \"7500\",\n" +
" \"MeasureUnitName\": \"كرتونة\",\n" +
" \"TotalPrice_Lc\": \"7500\",\n" +
" \"PricePerUnit_Lc\": \"75\",\n" +
" \"Quantity\": \"100\"\n" +
" }\n" +
" ],\n" +
" \"Id\": \"274\",\n" +
" \"OrderDate\": \"4/10/2014 12:00:00 AM\",\n" +
" \"Number\": \"16\",\n" +
" \"CustomerName\": \"الأسد\",\n" +
" \"Note\": \"\"\n" +
" }\n" +
"]";
Item[] placelist;
Gson gson = new Gson();
placelist = gson.fromJson(responseJSON, Item[].class);
System.out.println(Arrays.toString(placelist));
}
public class Item {
private String OrderDate;
private String Number;
private String Note;
private String CustomerName;
private String Id;
@SerializedName("_OrderDetails")
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;
}
@Override
public String toString() {
return "Item{" +
"OrderDate='" + OrderDate + '\'' +
", Number='" + Number + '\'' +
", Note='" + Note + '\'' +
", CustomerName='" + CustomerName + '\'' +
", Id='" + Id + '\'' +
", 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;
}
@Override
public String toString() {
return "_OrderDetails{" +
"OrderId='" + OrderId + '\'' +
", OrderDate='" + OrderDate + '\'' +
", Number='" + Number + '\'' +
", Note='" + Note + '\'' +
", ProductName='" + ProductName + '\'' +
", TotalAfterDiscount_Lc='" + TotalAfterDiscount_Lc + '\'' +
", MeasureUnitName='" + MeasureUnitName + '\'' +
", TotalPrice_Lc='" + TotalPrice_Lc + '\'' +
", PricePerUnit_Lc='" + PricePerUnit_Lc + '\'' +
", Quantity='" + Quantity + '\'' +
'}';
}
}
}
}
BTW this JSON's parsing is wrong. Your order details list will be null every single time.
"_OrderDetails": [ … ] in JSON means that list's name of orders is "_OrderDetails"