2015-07-20 67 views
1

我正在使用擁有getter和setter的POJO類。但是,由於這種動態行爲,我將被迫使用反序列化部分。我也已經實現了代碼,但是還有其他方法可以解決這個問題。因爲,下面粘貼響應是實際響應的僅有一小部分(這是大規模和我使用它POJO getter和setter)Retrofit - 處理具有動態結構的JSON密鑰 - Array/Object

OBJECT

secondaryUser: { 
    id: 1, 
    username: "admin", 
    numberOfFollowers: 1, 
    displayName: "admin" 
    } 

ARRAY

secondaryUser: [ 
    { 
    id: 18150, 
    activityDateTime: "2015-07-20 14:46:02", 
    user: { 
    id: 1, 
    username: "admin", 
    numberOfFollowers: 1, 
    displayName: "admin" 
    } 
    }, 
    { 
    id: 18148, 
    activityDateTime: "2015-07-20 13:35:02", 
    user: { 
    id: 3, 
    username: "USER_1", 
    numberOfFollowers: 4, 
    displayName: "USER_1" 
    } 
    }, 
    { 
    id: 18146, 
    activityDateTime: "2015-07-20 11:29:41", 
    user: { 
    id: 2468, 
    username: "USER_2", 
    numberOfFollowers: 1, 
    displayName: "USER_2" 
    } 
    } 
    ] 
    } 

回答

2

我希望這能解決你的問題。

首先,你的情況,如果你已經聲明secondaryUser爲對象或數組,將其更改爲List<SecondaryUser> secondaryUser

創建解串器。

DynamicJsonConverter.java

public class DynamicJsonConverter implements Converter { 

    private static String fromStream(InputStream in) throws IOException { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
     StringBuilder out = new StringBuilder(); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      out.append(line); 

     } 
     return out.toString(); 
    } 

    @Override 
    public Object fromBody(TypedInput typedInput, Type type) throws ConversionException { 
     try { 
      InputStream in = typedInput.in(); // convert the typedInput to String 
      String string = fromStream(in); 
      in.close(); // we are responsible to close the InputStream after use 
      return string; 

     } catch (Exception e) { // a lot may happen here, whatever happens 
      throw new ConversionException(e); // wrap it into ConversionException so retrofit can process it 
     } 

    } 

    @Override 
    public TypedOutput toBody(Object object) { // not required 
     return null; 
    } 
} 

你休息適配器類。

BasePathAdapterForDynamicJSONKeys.java

public class BasePathAdapterForDynamicJSONKeys { 
     private static RetroFitInterface topRecommendationsInterface; 

     public static RetroFitInterface getCommonPathInterface() { 

      RestAdapter restAdapter = new RestAdapter.Builder() 
        .setEndpoint(baseURL) 
        .setConverter(new DynamicJsonConverter()) 

        .build(); 
      topRecommendationsInterface = restAdapter.create(RetroFitInterface.class); 
      return topRecommendationsInterface; 
     } 

此外,使用回調爲Callback<String>(),而不是Callback<YourObject>()

現在,你的活動/片段內,改造回調的成功方法中,使用此。

@Override 
public void success(String myData, Response response) { 
JSONObject mainObject = null; 
mainObject = new JSONObject(myData); 

//You can also use JSONObject, depends on what type the response is 

JSONArray myJsonArray = mainObject.getJSONArray("yourkey"); 

最後,這個傾倒你的ArrayList內部。(這基本上是JSON數組轉換成ArrayList的:))

ArrayList<MyObj> menuDetails = new Gson().fromJson(myJsonArray.toString(), new TypeToken<List<MyObj>>(){}.getType());