Java 预期为 BEGIN_OBJECT 但在第 1 行第 2 列处为 BEGIN_ARRAY(小编辑)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23077649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 (little edit)
提问by user3534834
what I am having here is a web service that gives me the following JSON code :
我在这里拥有的是一个 Web 服务,它为我提供了以下 JSON 代码:
[
{
"_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 :
我已经为所有数据创建了一个带有 getter 和 setter 的 java 类(实体):
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
但我在 logcat 中遇到以下异常:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
please what am I doing wrong???
请问我做错了什么???
回答by vzamanillo
Looking at your code, the right way to get your item
objects is
查看您的代码,获取item
对象的正确方法是
Item[] placelist = gson.fromJson(responseJSON, Item[].class);
because your JSON is an item
object list [
(BEGIN_ARRAY)
因为你的 JSON 是一个item
对象列表[
(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.
抛出异常,因为 Gson 需要一个单项对象{
(BEGIN_OBJECT) 但它是一个数组。
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.
您不能以两种方式反序列化相同的 JSON,作为数组和作为对象,如果您的 JSON 是一个数组,请将其反序列化为一个数组,如果您的 JSON 是一个对象,请将其反序列化为一个对象,但您不能以两种方式反序列化.
回答by Toochka
Your JSON output is a list.
您的 JSON 输出是一个列表。
// This parses your JSON
Item[] items = new Gson().fromJson(responseJson, Item[].class);
After this you'll have array of items
在此之后,您将拥有一系列项目
回答by Moses
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:
实际上,因为你有一个对象数组而不是一个对象。这就是为什么第二行有效而第三行无效的原因。因此,如果您想将此 json 解析为 Item[] 位置列表;你不需要改变任何东西。如果你想把它解析为一个对象,你应该删除这样的括号:
{
"_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:
工作正常,你有一个项目数组。但是您对 OrderDetails 的名称有疑问。在 json 中,它是“_OrderDetails”,在您的代码中它是“orderDetails”。您可以在字段上添加注释:
@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 + '\'' +
'}';
}
}
}
}
}
回答by Toochka
BTW this JSON's parsing is wrong. Your order details list will be null every single time.
顺便说一句,这个 JSON 的解析是错误的。您的订单详细信息列表每次都将为空。
"_OrderDetails": [ … ] in JSON means that list's name of orders is "_OrderDetails"
"_OrderDetails": [ ... ] 在 JSON 中表示订单列表的名称是 "_OrderDetails"
回答by Toochka
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.
编辑。使用下面的代码片段,您可以将 JSON 反序列化为Item
对象。
Gson gson = new Gson();
Item item = gson.fromJson(json, Item.class);