04-09 12:16:01.679 560 4-5604/?v/retrofit错误:retrofit.converter.conversionexception:com.google.gson.jsonsyntaxexception:java.lang.IllegalStateException:预期的BEGIN_ARRAY但BEGIN_OBJECT位于第1行第2列路径$
这是JSON响应(在GET请求后返回ok):
{
objects: [
{
rating: 0.97,
name: "High Line",
ranking: 0,
url: "http://www.thehighline.org",
price: null,
phone: "2125006035",
last_sync: 1428328869,
photos: [
"https://irs3.4sqi.net/img/general/original/11402168_2zKtnTfWXPJJJAaX7N6g1EMPTR7ahNqSAOsMotN-jNU.jpg"
],
local_id: 13778,
likes: 0,
city_id: 2621,
address: "btwn Gansevoort & W 34th St",
resource_uri: "/api/v1/venues/40f1d480f964a5206a0a1fe3/",
id: "40f1d480f964a5206a0a1fe3",
categories: [
{
name: "Park",
parent: {
local_id: 7,
name_id: "sights",
name: "Landmarks",
id: "4d4b7105d754a06377d81259"
},
local_id: 494,
name_id: "park",
category_id: 7,
id: "4bf58dd8d48988d163941735"
}
],
location: {
lat: 40.7470618874989,
lng: -74.0051937103271
}
},
{
rating: 0.97,
name: "Central Park",
ranking: 0,
url: "http://www.centralparknyc.org",
price: null,
phone: "2123106600",
last_sync: 1428521923,
photos: [
"https://irs2.4sqi.net/img/general/original/655018_Zp3vA90Sy4IIDApvfAo5KnDItoV0uEDZeST7bWT-qzk.jpg"
],
local_id: 13826,
likes: 0,
city_id: 2621,
address: "59th St to 110th St",
resource_uri: "/api/v1/venues/412d2800f964a520df0c1fe3/",
id: "412d2800f964a520df0c1fe3",
categories: [
{
name: "Park",
parent: {
local_id: 7,
name_id: "sights",
name: "Landmarks",
id: "4d4b7105d754a06377d81259"
},
local_id: 494,
name_id: "park",
category_id: 7,
id: "4bf58dd8d48988d163941735"
}
],
location: {
lat: 40.7888599444948,
lng: -73.9611625671387
}
}
],
meta: {
total_count: 1344,
next: "/api/v1/venues/?city_id=2621&category=topPicks&offset=2&limit=2&format=json",
limit: 2,
offset: 0
}
}
这是对改型服务的主要活动调用:
Map<String, String> params = new HashMap<String, String>();
params.put("city_id", "2621");
params.put("offset", "0");
params.put("limit", "2");
params.put("category", "topPicks");
params.put("format", "json");
ApiClient.getApiClient().listVenues(params, new Callback<List<ApiResponse>>() {
@Override
public void success(List<ApiResponse> venues, Response response) {
//consumir venues
Log.v("RETROFIT SUCCESS", response.getBody().toString());
mAdapter = new MainCustomAdapter(venues);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void failure(RetrofitError retrofitError) {
if (retrofitError.getResponse() != null) {
Log.v("Retrofit error", retrofitError.getCause().toString());
}
//manejar el fallo
}
});
public class ApiClient {
private static ApiVenuesInterface apiVenues;
public static ApiVenuesInterface getApiClient() {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
if (apiVenues == null) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://endpoint.com")
.setConverter(new GsonConverter(gson))
.setLogLevel(RestAdapter.LogLevel.FULL).setLog(new AndroidLog("RETROFIT"))
.build();
apiVenues = restAdapter.create(ApiVenuesInterface.class);
}
return apiVenues;
}
public interface ApiVenuesInterface {
//llamada asíncrona al querystring de venues
@GET("/api/v1/venues")
void listVenues(@QueryMap Map<String, String> params, Callback<List<ApiResponse>> callback);
}}
public class ApiResponse {
private List<Object> objects = new ArrayList<Object>();
}
class Object {
@Expose
private String name;
@Expose
private List<String> photos = new ArrayList<String>();
@Expose
private String address;
@Expose
private List<Category> categories = new ArrayList<Category>();
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @return
* The photos
*/
public List<String> getPhotos() {
return photos;
}
/**
*
* @return
* The address
*/
public String getAddress() {
return address;
}
/**
*
* @return
* The categories
*/
public List<Category> getCategories() {
return categories;
}
class Category {
@Expose
private String name;
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
}}
重要编辑:此问题对改型1.x有效。请注意,Retrifit2.x与此略有不同,因为它使用了注释和调用方法。
无法弄清楚如何将以下带有GSON的JSON字符串解析为任何适当的对象以检索其数据。字符串是: 我发现方括号表示它是一个ArrayList,但里面还有另一个对象。 如果我试着在ArrayList上搜索: 然后它抱怨: 如果我尝试相反的方法: 然后它也在抱怨: 不理解该格式:-/ 感谢您的任何建议!
我正在使用来解析Json数据。我的Json数据如下: GsonParse.java 我使用以下方法来解析此JSON数据。 我面对以下错误。
其答复如下: 我相信问题出在响应前面的jsonFlickrApi上。 执行以下代码时: }
我有一个非常简单的产品评论JSON,比如: 我想用GSON把它读到我的Java应用程序中。我构建了一个类来保存每次复习的结果: 使用这段代码,我只能检索JSON中的第一个评论,所以我的问题是:如何遍历所有阅读器并获得下一个评论?我不需要将评论存储在列表中,只需要访问对象一次。任何帮助都大于欢迎。
我正在尝试将JSON转换为GSON,我不确定这是最好的结构。 所有响应都由代码、消息和数据结构组成。但数据的内部结构可能会有所不同。 这是我的回应对象