我的 JSON 输出有什么问题? retrofit.converter.ConversionException
What's wrong with my JSON Output? retrofit.converter.ConversionException
我正在尝试对一些 json 进行 retro fit,但看不出我的 json 字符串有什么问题,我不断收到同样的错误。
我期待一个单词对象列表,但我错过了什么?有什么想法吗?
retrofit.converter.ConversionException:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
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"
}
]
}
改造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接口:
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);
}
JSON 的单词对象列表嵌入在具有 "words" 属性 的对象的值中。该错误只是指出,它期待一个数组定界符 [
而不是它找到了该对象的开头。
您可以修复 JSON(删除 {"words": <ACTUAL_ARRAY> }
并只留下两个元素的数组)或修改您在 getRestWordFeed 中期望的数据结构。
您的 JSON 是一个具有一个属性的对象:'words' 这是一个词对象数组。您的错误:"Expected BEGIN_ARRAY but was BEGIN_OBJECT" 表示您需要一个数组,但 return 编辑了一个对象。一种解决方案是 return 以下 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"
}
]
我正在尝试对一些 json 进行 retro fit,但看不出我的 json 字符串有什么问题,我不断收到同样的错误。
我期待一个单词对象列表,但我错过了什么?有什么想法吗?
retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
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"
}
]
}
改造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接口:
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);
}
JSON 的单词对象列表嵌入在具有 "words" 属性 的对象的值中。该错误只是指出,它期待一个数组定界符 [
而不是它找到了该对象的开头。
您可以修复 JSON(删除 {"words": <ACTUAL_ARRAY> }
并只留下两个元素的数组)或修改您在 getRestWordFeed 中期望的数据结构。
您的 JSON 是一个具有一个属性的对象:'words' 这是一个词对象数组。您的错误:"Expected BEGIN_ARRAY but was BEGIN_OBJECT" 表示您需要一个数组,但 return 编辑了一个对象。一种解决方案是 return 以下 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"
}
]