[英]What's wrong with my JSON Output? retrofit.converter.ConversionException
I'm trying to use retro fit with some json but can't see what's wrong with my json string, i keep getting the same error. 我正在尝试对某些json使用Retro Fit,但看不到json字符串出了什么问题,我不断收到相同的错误。
I'm expecting an list of word objects but ive missed something? 我期待一个单词对象列表,但是我错过了什么? Any ideas? 有任何想法吗?
retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ retrofit.converter.ConversionException:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列路径$处为BEGIN_OBJECT
JSON: JSON:
{
"words": [
{
"id": "1",
"word": "submarine",
"word_syllables": "sub-mar-ine",
"picture": "none.jpg",
"picture_dir": "",
"soundfile": "",
"user_id": "1",
"created": "2015-04-08 16:32:07",
"modified": "0000-00-00 00:00:00"
},
{
"id": "2",
"word": "computer",
"word_syllables": "com-pute-r",
"picture": "computer.jpg",
"picture_dir": "",
"soundfile": "",
"user_id": "0",
"created": "2015-04-08 16:32:07",
"modified": "0000-00-00 00:00:00"
}
]
}
Retrofit Android code: 改造Android代码:
private void requestData(){
RestAdapter adapter=new RestAdapter.Builder()
.setEndpoint(ENDPOINT)
.build();
WordsAPI api=adapter.create(WordsAPI.class);
api.getRestWordFeed(new Callback<List<Word>>(){
@Override
public void failure(RetrofitError arg0) {
// TODO Auto-generated method stub
Log.v("error",arg0.getMessage());
}
@Override
public void success(List arg0, Response arg1) {
// TODO Auto-generated method stub
wordList=arg0;
printList();
}
});
}
API interface: API接口:
package com.example.testretrofitlib;
import java.util.List;
import retrofit.Callback;
import retrofit.http.GET;
public interface WordsAPI {
@GET("/rest_words/index.json")
public void getRestWordFeed(Callback<List<Word>> response);
}
Your JSON is an object that has one attribute: 'words' which is an array of word objects. JSON是一个具有一个属性的对象:'words'是一个单词对象数组。 Your error: "Expected BEGIN_ARRAY but was BEGIN_OBJECT" is indicating that you are expecting an array, but returned an object. 您的错误:“预期为BEGIN_ARRAY,但之前为BEGIN_OBJECT”表示您期望使用数组,但返回了一个对象。 One solution would be to return the following as json: 一种解决方案是将以下内容作为json返回:
[
{
"id": "1",
"word": "submarine",
"word_syllables": "sub-mar-ine",
"picture": "none.jpg",
"picture_dir": "",
"soundfile": "",
"user_id": "1",
"created": "2015-04-08 16:32:07",
"modified": "0000-00-00 00:00:00"
},
{
"id": "2",
"word": "computer",
"word_syllables": "com-pute-r",
"picture": "computer.jpg",
"picture_dir": "",
"soundfile": "",
"user_id": "0",
"created": "2015-04-08 16:32:07",
"modified": "0000-00-00 00:00:00"
}
]
The list of words object of that JSON is embedded inside the value of an object with a "words" property. 该JSON的单词对象列表被嵌入带有“单词”属性的对象的值内。 That error just points out that, it was expecting an array delimiter [
instead it found the start of that object. 该错误只是指出,它期望使用数组定界符[
而是找到了该对象的开始。
You could either fix the JSON (removing that {"words": <ACTUAL_ARRAY> }
and just leaving the array with two elements) or modify the data structure you expect in getRestWordFeed. 您可以修复JSON(删除{"words": <ACTUAL_ARRAY> }
并仅在数组中保留两个元素),也可以修改getRestWordFeed中期望的数据结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.