Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

java - Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 (little edit)

what I am having 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": ""
  }
]

and I have 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;
    }

}

}

and in the main activity I get the data like this:

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

but I get in the logcat the following exception : Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2

please what am I doing wrong???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...