如何使用改进来解析JSON数组

1 java android json retrofit

所以我有一个改造界面:

public interface RestUserInformation {
    @GET("/api/me")
    void getInfo(Callback<UserInformation> callback);
}
Run Code Online (Sandbox Code Playgroud)

一个RestAdapter:

RestAdapter userInformation = newRestAdapter.Builder()
                              .setEndpoint(IP_ADDRESS)
                              .setRequestInterceptor(requestInterceptor)
                              .build();
Run Code Online (Sandbox Code Playgroud)

这使得API调用和接收JSON正文答案如下:

{"user":{ 
     "id":50,
     "email":"mail@mail.com",
     "first_name":"chris",
     "last_name":"cr",
     "birthdate":"1817-03-04",
     "gender":1,
     "country":"fr"
     {     
     "id":8,
     "language":"Spanish",
     "flag_path":"public/images/flags/Argentina.ico",
     "created_at":"2014-11-05T20:42:39.294Z",
     "updated_at":"2014-11-05T20:42:39.294Z",
     "name":"Argentina","available":false,
     "i18n_key":null
     }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想解析它并用它填充一个类,所以我创建了UserInformation.java类:

public class UserInformation {
    int id;
    String email;
    String first_name;
    String last_name;
    String birthdate;
    int gender;
    String country;  }
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用回调函数:

        RestUserInformation getUserInfo = userInformation.create(RestUserInformation.class);
        getUserInfo.getInfo(new Callback<UserInformation>() {
               @Override
               public void success(UserInformation user, Response response) {
                                            }

               @Override
               public void failure(RetrofitError error) {
                                            }
                                        });
Run Code Online (Sandbox Code Playgroud)

我尝试了很多东西,但它不起作用,UserInformation类的属性保持空白..有关如何做的任何想法?

Tan*_*.7x 5

默认情况下,Retrofit使用GSON来解析JSON.

GSON尝试使用1:1映射将JSON字段映射到Java对象 - 这是Java类的结构必须与JSON的结构匹配才能执行自动解析.

这里的问题是你的JSON不是一个单独的对象 - 它是另一个对象中的一个对象(标记为"user").

要解决此问题,您可以创建另一个类来封装外部对象,也可以为GSON创建自定义反序列化程序.

外部对象的包装类示例:

public class UserResponse {
    public UserInformation user;
}
Run Code Online (Sandbox Code Playgroud)

以下是自定义反序列化程序的示例,如果您想沿着该路线前进:

public class UserInformationAdapter implements JsonDeserializer<UserInformation> {
    @Override
    public UserInformation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        // Get the root JSON object
        JsonObject rootObject = json.getAsJsonObject();

        // Get the user JSON element
        JsonElement userElement = rootObject.get("user");

        // Let GSON try to automatically deserialize the user element.
        return context.deserialize(userElement, UserInformation.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在Retrofit中注册此类型的适配器,如下所示:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(UserInformation.class, new UserInformationAdapter());
Gson gson = gsonBuilder.create();
RestAdapter adapter = new RestAdapter.Builder()
    .setConverter(new GsonConverter(gson))
    .build();
Run Code Online (Sandbox Code Playgroud)

这样,GSON将使用您UserInformationAdapter的反序列化JSON 而不是尝试使用其默认解析策略.