本文介绍了使用moshi对JSON API响应进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反序列化json响应后,得到了null对象属性.在android下开发,我使用的是retrofit2,moshi作为转换器( https://github.com/kamikat/moshi-jsonapi ).调试时,我看到完全检索了json响应(不是null属性),但是反序列化失败.我应该改用GSON吗?

I got a null object attributes after deserialization of a json response.Developing under android, I'm using retrofit2 , moshi as converter (https://github.com/kamikat/moshi-jsonapi ) .When debugging ,I saw a json response fully retrieved (not null attributes),but deserialization fails. Should I use GSON instead?

这是我用来进行json呼叫的改造生成器:(没问题)

Here's my retrofit builder I use to make my json call: (no issue)

public static JsonServerInterface getSimpleClient(){

     Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_AUTH_URL)a
            .addConverterFactory(MoshiConverterFactory.create())
            .build();
   JsonServerInterface webServer=retrofit.create(JsonServerInterface.class);
   return webServer;
}

我的api json调用,响应包含具有null属性的UserModel(反序列化失败,没有任何错误)

My api json call,response contain UserModel with null attributes(deserialization fails without any error)

signInCall.enqueue(new Callback<UserModel>(){
  @Override
  public void onResponse
  (Call<UserModel> call, Response<UserModel> response)
  {
    response.message();
  }
}

我的UserModel(按moshi的要求,但我认为它缺少某些东西):

My UserModel (as required by moshi ,but I think it lacks something):

@JsonApi(type = "users")
public class UserModel extends Resource {

@Json(name = "auth-token")
private String authToken;
@Json(name = "firstname")
private String firstname;
@Json(name = "lastname")
private String lastname;
@Json(name = "email")
private String email;
@Json(name = "created-at")
private String createdAt;
@Json(name = "updated-at")
private String updatedAt;

private HasMany<ActivityModel> activities;

我的json响应是在调试http响应时看到的,我检索时没有任何麻烦,但是moshi很烂地反序列化了它,并且没有出现错误:

My json response I saw when debugging http response, I retrieve without any trouve,but moshi sucks to deserialize it,and no errors are raised:

{
    "data": {
        "id": "21",
        "type": "users",
        "attributes": {
            "auth-token": "t8S3BTqyPwN3T4QDMY1FwEMF",
            "firstname": "aymen",
            "lastname": "myself",
            "email": "[email protected]",
            "created-at": "2017-11-13T22:52:39.477Z",
            "updated-at": "2017-11-13T23:21:09.706Z"
        },
        "relationships": {
            "activities": {
                "data": [
                    {
                        "id": "81",
                        "type": "activities"
                    }
                ]
            }
        }
    },
    "included": [
        {
            "id": "81",
            "type": "activities",
            "attributes": {
                "title": "activity 10",
                "description": "how to draw a circle",
                "start-at": "2017-11-13T23:06:13.474Z",
                "duration": 10,
                "created-at": "2017-11-13T23:06:32.630Z",
                "updated-at": "2017-11-13T23:06:32.630Z"
            },
            "relationships": {
                "user": {
                    "data": {
                        "id": "21",
                        "type": "users"
                    }
                }
            }
        }
    ]
}

推荐答案

很多小时后我找到了解决方案:我应该使用文档"而不是UserModel

I find the solution after lot of hours:I should use "Document" instead of UserModel

界面:

 @POST("sign-in.json")
    Call<Document> signIn(@Body Credentials credentials);

呼叫时:

   signInCall.enqueue(new Callback<Document>(){
            @Override
            public void onResponse(Call<Document> call, Response<Document> response) {

希望有帮助

这篇关于使用moshi对JSON API响应进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:34