gpt4 book ai didi

java - JSON解析异常 : Expected BEGIN_ARRAY but was BEGIN_OBJECT?

转载 作者:行者123 更新时间:2023-11-29 05:39:23 26 4
gpt4 key购买 nike

我正在尝试解析 the JSON response from this link我得到了这个异常(exception):

Expected BEGIN_ARRAY but was BEGIN_OBJECT

我创建了这个类来封装 JSON 数据:

public class PlacesTextSearch {

private String icon;
private String name;
private String types;
private String formatted_address;
private double latitude;
private double longitude;
private String id;

public PlacesTextSearch(String icon,String name,String types,String formatted_address,double latitude,double longitude) {
// TODO Auto-generated constructor stub
setIcon(icon);
setName(name);
setTypes(types);
setFormatted_address(formatted_address);
setLatitude(latitude);
setLongitude(longitude);
}

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTypes() {
return types;
}
public void setTypes(String types) {
this.types = types;
}
public String getFormatted_address() {
return formatted_address;
}
public void setFormatted_address(String formatted_address) {
this.formatted_address = formatted_address;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
}

这是我解析 JSON 的代码:

private ArrayList<PlacesTextSearch> arrayListPlaces;       
Type listType = new TypeToken<ArrayList<PlacesTextSearch>>(){}.getType();
arrayListPlaces=new Gson().fromJson(response,listType);

在这里你可以看到完整的异常堆栈跟踪:

Complete exception stacktrace

最佳答案

你读过Gson documentation吗?在尝试编写代码之前?你有没有看过JSON structures

您的代码有很多错误...关键是您必须创建一个与您的 JSON 结构相匹配的 Java 类结构。事实上,您的类结构甚至与您要解析的 JSON 都不相似!基本上哪里有一个对象 { }在你的 JSON 中你必须使用一个类,并且在你的 JSON 中有一个数组 [ ]你必须使用数组或 List ...

根据您的PlacesTextSearch类,我想你要解析的 JSON fragment 是:

{    
...,
"results" : [
{
"formatted_address" : "Zeytinlik Mh., Bakırköy/İstanbul, Türkiye",
"geometry" : {
"location" : {
"lat" : 40.9790040,
"lng" : 28.8730110
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "e520b6e19bae5c014470989f9d3405e55dce5155",
"name" : "PTT",
"types" : [ "post_office", "finance", "establishment" ]
...
},
...
],
...
}

那么,你如何假装将其解析为 ArrayList<PlacesTextSearch> !?那不是您的 JSON 所代表的!你真的没看到吗?

尝试像这样的类结构(伪代码):

class Response
List<PlacesTextSearch> results;

class PlacesTextSearch
String formatted_address;
Geometry geometry;
String icon;
String id;
String name;
List<String> types;

class Geometry
Location location;

class Location
long lat;
long lng;

然后解析它:

Response response = new Gson().fromJson(response, Response.class);

关于java - JSON解析异常 : Expected BEGIN_ARRAY but was BEGIN_OBJECT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18208618/

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