改造 Gson:预期 BEGIN_ARRAY 但为 BEGIN_OBJECT(不重复)

Retrofit Gson : Expected BEGIN_ARRAY but was BEGIN_OBJECT (Not duplicate)

拜托,我尝试了两种方法来解决这个问题,但都不起作用,我不知道发生了什么,但在它没有问题之前。 我有以下 json :

{
  "_embedded" : {
    "posts" : [ {
      "postid" : 1,
      "taggedusers" : null,
      "hashtags" : null,
      "createdAt" : "2016-05-07",
      "commentsCount" : 0,
      "likesCount" : 0,
      "shareCount" : 0,
      "description" : "nothing",
      "post" : "nothing",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/posts/1"
        },
        "postsEntity" : {
          "href" : "http://localhost:8080/posts/1"
        },
        "usersByUserid" : {
          "href" : "http://localhost:8080/posts/1/usersByUserid"
        },
        "commentsesByPostid" : {
          "href" : "http://localhost:8080/posts/1/commentsesByPostid"
        }
      }
    }, {
      "postid" : 2,
      "taggedusers" : null,
      "hashtags" : null,
      "createdAt" : "2016-05-07",
      "commentsCount" : 0,
      "likesCount" : 0,
      "shareCount" : 0,
      "description" : "nothing",
      "post" : "nothing",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/posts/2"
        },
        "postsEntity" : {
          "href" : "http://localhost:8080/posts/2"
        },
        "usersByUserid" : {
          "href" : "http://localhost:8080/posts/2/usersByUserid"
        },
        "commentsesByPostid" : {
          "href" : "http://localhost:8080/posts/2/commentsesByPostid"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/users/6/postsesByUserid"
    }
  }
}

我尝试在改造中使用以下内容阅读它:

 Retrofit retrofit = RetrofitGenerator.initRetrofit(CacheUtils.readSharedPreference(getActivity()).getToken());
        PostService postService= retrofit.create(PostService.class);
         postService.getUsersPost((int) CacheUtils.readSharedPreference(getActivity()).getUserID()).enqueue(new Callback<List<Post>>() {
             @Override
             public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                 if(response.isSuccessful()){
                     List<Post> postEntitiyList = response.body();

                 }
             }

             @Override
             public void onFailure(Call<List<Post>> call, Throwable t) {
                 Log.e("teeest",t.getMessage());
             }
         });

和接口:

public interface PostService {
    @Headers("Content-Type:application/json")
    @GET("/posts")
    Call<List<Post>> getAllPosts();

    @Headers("Content-Type:application/json")
    @GET("users/{id}/postsesByUserid")
    Call<List<Post>> getUsersPost(@Path("id") int id);
}

但我遇到了这个问题:预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $=BEGIN_OBJECT 我试着在另一个 class 中制作 Post Pojo,如下所示:

public class PostEntitiy {
    private Post post;

    public PostEntitiy(Post post) {
        this.post = post;
    }

    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }
}

然后我用 List 仍然遇到同样的问题,我用错了什么?

Expected BEGIN_ARRAY but was BEGIN_OBJECT

您的 JSON 不是列表,它是一个对象:{ ... } => 对象,[ ... ] => 列表。

编辑

入门指南:

class Embedded {
    ...
}

class Links {
    ...
}

class Top {
    Embedded _embedded;
    Links _links;
}

等等等等。

然后您所有的 List<Post> 都应替换为 Top

更改我的 类 的命名以适合您的风格。