2017-07-07 66 views
1

如何轉換給定的JSON響應如何使用Retrofit2解析JSON成類的子類(ES)

{ 
    "name" : "John", 
    "surname" : "Doe", 
    "location" : { 
     "name" : "Paris", 
     "desc" : "Welcome to Paris" 
    } 
} 

class Person 
{ 
    String name; 
    String surname; 
    Location location; // new Location(String name, String desc) 
} 

這是所有關於嵌套Location類裏面Person

+1

使用本站生成JSON的類 –

回答

3

使用@Expose@SerializedName註釋類似

class Person 
{ 
    @SerializedName("name") 
    String name; 

    @SerializedName("surname") 
    String surname; 

    @SerializedName("location") 
    Location location; // new Location(String name, String desc) 
} 

Location類等

class Location 
    { 
     @SerializedName("name") 
     String name; 

     @SerializedName("desc") 
     String desc; 
    } 

添加getter和setter方法用於訪問數據

1

使用GSON +改造的組合。

首先使用Retrofit在模型類的字段中提供的註釋@SerializedName("yourFieldName")

初始化您GSON配置有RuntimeTypeAdapterFactory

RuntimeTypeAdapterFactory<Person> itemFactory = RuntimeTypeAdapterFactory 
     .of(Person.class) // The field that defines the type 
     .registerSubtype(Location.class, "location") 
     .registerSubtype(YourSubclass.class) // if the flag equals the class name, you can skip the second parameter. 

Gson gson = new GsonBuilder() 
     .registerTypeAdapterFactory(itemFactory) 
     .create(); 

然後你初始化改造:

Retrofit.Builder builder = new Retrofit.Builder(); 
builder.baseUrl(BASE_URL); 
builder.addConverterFactory(GsonConverterFactory.create(gson)); 

Retrofit retrofit = builder.build(); 
+0

過於複雜相比第一響應,但仍然有用 –

+0

該方法使整個系統動態。它允許您使用Retrofit實例自動實例化不同的對象。試一試 :) –

1

首先得到的API,並獲得通過提前REST客戶端的JSON輸出,是一款Chrome擴展。現在把這個輸出到JSON to POJO converter ,你會得到你的POJO類。將它們粘貼到您的項目中。做一個interface`

/** 
* Get Data 
* 
* @param body Holds the JSON payloads 
* @return Formatted data 
*/ 
@POST("JobSpotAPI/getUserInterviewSchedule") 
Call<POJOClass> getData(@Body JsonObject body); 

和安裝客戶端

public class RetrofitClient { 
private static Retrofit retrofit = null; 

public static Retrofit getClient(String baseUrl) { 
    if (retrofit==null) { 
     retrofit = new Retrofit.Builder() 
       .baseUrl(baseUrl) 
       .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
    } 
    return retrofit; 
} 

創建回收站視圖設置它的適配器和所有。只需添加一些新的方法,如 -

public void updateAnswers(List<Item> items) { 
    mItems = items; 
    notifyDataSetChanged(); 
} 

private Item getItem(int adapterPosition) { 
    return mItems.get(adapterPosition); 
} 

Setup Utility類來調用接口。

public class ApiUtils { 

public static final String BASE_URL = "https://base_url/"; 

public static Interface_name methodName() { 
    return RetrofitClient.getClient(BASE_URL).create(Interface_name.class); 
}} 

聲明接口在活動

Interface_name obj = ApiUtils.methodName(); 

如果你有一些有效載荷然後附上他們給你提出要求。在此之前,你需要一個JSON字符串傳遞給API請求。製作JSON有效內容。

private JsonObject makeJsonObjectPayload() { 
    JsonObject requestBean = new JsonObject(); 
    requestBean.addProperty("key", value); 
    requestBean.addProperty("key", value); 
    requestBean.addProperty("key", value); 
    requestBean.addProperty("key", value); 
    return requestBean; 

根據JSON響應http://www.jsonschema2pojo.org/傳遞API請求

​​