gpt4 book ai didi

java - 我的 JSON 输出有什么问题? retrofit.converter.ConversionException 异常

转载 作者:行者123 更新时间:2023-11-29 07:41:30 25 4
gpt4 key购买 nike

我正在尝试对一些 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"
}
]
}

改造安卓代码:

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):

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',它是一个单词对象数组。您的错误:“Expected BEGIN_ARRAY but was BEGIN_OBJECT”表示您需要一个数组,但返回了一个对象。一种解决方案是将以下内容作为 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"
}
]

关于java - 我的 JSON 输出有什么问题? retrofit.converter.ConversionException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29685643/

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