gpt4 book ai didi

java - 预期为 BEGIN_OBJECT 但在第 1 行第 2 列为 BEGIN_ARRAY(小编辑)

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:19 24 4
gpt4 key购买 nike

我在这里拥有的是一个网络服务,它为我提供了以下 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;
}

}

}

在主要 Activity 中,我得到这样的数据:

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

但我在 logcat 中得到以下异常: 应为 BEGIN_OBJECT 但在第 1 行第 2 列为 BEGIN_ARRAY

请问我做错了什么???

最佳答案

查看您的代码,获取 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 是对象,则将其反序列化为对象,但您不能反序列化两种方式。

关于java - 预期为 BEGIN_OBJECT 但在第 1 行第 2 列为 BEGIN_ARRAY(小编辑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23077649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com