【问题标题】:What's wrong with my JSON Output? retrofit.converter.ConversionException我的 JSON 输出有什么问题?改造.converter.ConversionException
【发布时间】:2015-04-16 20:48:56
【问题描述】:

我正在尝试对一些 json 使用复古拟合,但看不到我的 json 字符串有什么问题,我一直收到同样的错误。

我期待一个单词对象列表,但我错过了什么?有什么想法吗?

retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException:java.lang.IllegalStateException: 预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT

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


}

【问题讨论】:

  • 您期待一个 List 但 JSON 有一个对象 Word,其中包含一个数组。

标签: java android json gson retrofit


【解决方案1】:

您的 JSON 是一个具有一个属性的对象:“words”,它是一组单词对象。您的错误:“预期 BEGIN_ARRAY 但为 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"
    }
]

【讨论】:

    【解决方案2】:

    该 JSON 的单词列表对象嵌入在具有“单词”属性的对象的值中。该错误只是指出,它期待一个数组分隔符[,而不是它找到了该对象的开头。

    您可以修复 JSON(删除 {"words": &lt;ACTUAL_ARRAY&gt; } 并只留下两个元素的数组)或修改您期望在 getRestWordFeed 中的数据结构。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 2010-11-09
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多