简体   繁体   中英

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.

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 $

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:

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


}

Your JSON is an object that has one attribute: 'words' which is an array of word objects. Your error: "Expected BEGIN_ARRAY but was BEGIN_OBJECT" is indicating that you are expecting an array, but returned an object. One solution would be to return the following as 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM