我在这里提供的是一个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": ""
}
]
我已经为所有数据制作了一个带有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;
}
}
}
在主要活动中,我得到这样的数据:
Item[] placelist;
placelist = gson.fromJson(responseJSON, Item[].class);
Item item = gson.fromJson(responseJSON, Item.class);
但是我在logcat中遇到以下异常:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
请问我做错了什么???
答案 0 :(得分:9)
查看代码,获取item
对象的正确方法是
Item[] placelist = gson.fromJson(responseJSON, Item[].class);
因为您的JSON是item
对象列表[
(BEGIN_ARRAY)
Item item = gson.fromJson(responseJSON, Item.class);
引发异常,因为Gson期望单个项目对象{
(BEGIN_OBJECT)但是是一个数组。
您无法以两种方式反序列化相同的JSON,如数组和对象,如果您的JSON是数组,则将其反序列化为数组,如果JSON是对象,则将其反序列化为对象,但不能反序列化两种方式。
答案 1 :(得分:2)
您应该如何定义班级:
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
}
}
编辑。使用下面的代码段,您可以将JSON反序列化为Item
对象。
Gson gson = new Gson();
Item item = gson.fromJson(json, Item.class);
答案 2 :(得分:1)
您的JSON输出是一个列表。
// This parses your JSON
Item[] items = new Gson().fromJson(responseJson, Item[].class);
在此之后你将拥有一系列项目
答案 3 :(得分:1)
实际上,因为你有一个对象数组而不是一个对象。这就是为什么第二行有效,第三行没有。所以如果你想把这个json解析成Item []的地方列表;你不需要改变任何东西。如果要将其解析为Object,则应删除如下括号:
{
"_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": ""
}
或者像数组一样解析它并检索第一个元素。
更新
Item[] placelist = gson.fromJson(responseJSON, Item[].class);
工作正常,你有一系列的项目。但是OrderDetails的名称有问题。在json中,代码中的“_OrderDetails”是“orderDetails”。您可以在字段上添加注释:
@SerializedName("_OrderDetails")
private List<_OrderDetails> orderDetails;
要测试的完整代码:
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 + '\'' +
'}';
}
}
}
}
答案 4 :(得分:0)
BTW这个JSON的解析是错误的。您的订单详细信息列表每次都为空。
“_ OrderDetails”:[...]在JSON中表示订单的名称是“_OrderDetails”