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

标签 java android json gson retrofit

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

相关文章:

java - 将数据从服务发送到 Activity

Android:使用 DrawPoint() 的 onDraw 方法导致 Canvas 上的抖动和失真

java - 序列化多态对象列表时未设置类型 ID 字段

java - 移除或删除现有 Websphere Application Server 的配置文件

java - 在 Java 中单击 g.drawString() 时更改 boolean 值

java - 如何使用 createRow() 将行添加到 View 对象后删除行

java - 在 Java 中使用键绑定(bind)和操作映射作为按钮的快捷键

java - Android Studio 无法解析符号

json - 为什么 $.each 说这个 JSON 对象未定义?

php - Select2 jQuery 与 JSON MySQL