2017-07-15 3 views
1

からクラスを区切る方法:私は、個々のクラスとしての行動 'にアクセスするために改造を使用しようとしているAndroidのレトロフィット - 次の中からJSONデータにアクセスしようとJSON配列

{"actions":[{"actionType":0,"email":"[email protected]","faIcon":"fa-envelope", 
"name":"Contact Us","subject":"Email from Tony's Pizza App"}, 
{"actionType":2,"faIcon":"fa-phone","name":"Call Us","number":"5204558897"}], 
"total":2} 

。 (ActionEmail、ActionPhoneなど)。私はこれらを別々のクラスに分け、すべてのプロパティを持つクラスを一つも持たない方法を考え出すことはできません。

ありがとうございます!ここで

+0

GsonのようなJsonパーサを改造して使用していますか? –

+0

[こちら](http://www.pratikbutani.com/2016/05/android-tutorial-json-parsing-using-retrofit-part-1/)を確認してください。 – sHOLE

+0

はい、Gson w/Retrofitを使用しています。すべてのデータを1つのアクションクラスに配置したいのですが、私の目標は各アクションタイプに基づいて個別のクラスを作成することです。 I:actionType = 0の場合、actionType = 2のActionEmailクラスを使用し、ActionCallなどを使用します。 迅速な対応に感謝します。 –

答えて

0
Call<ActionWrapperObject> getActions(// Put your api call body in there); 

ここにあなたのActionWrapperObject

public class ActionWrapperObject { 
    ArrayList<ActionModel> actions; 

    public ArrayList<ActionModel> getActions() { 
     return actions; 
    } 

    public void setActions(ArrayList<ActionModel> actions) { 
     this.actions = actions; 
    } 
} 

はあなたの応答に

Your api call.enqueue(new Callback<ActionWrapperObject>() { 
        @Override 
        public void onResponse(Call<ActionWrapperObject> call, Response<ActionWrapperObject> response) { 
         ActionWrapperObject actionWrapperObj= response.body(); 
         if (actionWrapperObj!= null) { 
          ArrayList<ActionModel> actionModelList= actionWrapperObj.getActions(); 
//Here you got the list of actions. Do what ever you want with them. You can 
// differentiate each action on its type. 
                } 
        } 
+0

これは良いことですが、同様のソリューションを実装しました。ただし、これはActionTypesの2つだけにも基づいています... ActionTypesの量を増やしたいのですが?このクラスは大規模になりませんか?数多くのactionTypesに成長し始めると間違って見えます... 返信ありがとう! –

0

あなたActionModel

public class ActionModel { 

    int actionType; 
    String email; 
    String faIcon; 
    String name; 
    String subject; 

    public int getActionType() { 
     return actionType; 
    } 

    public void setActionType(int actionType) { 
     this.actionType = actionType; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getFaIcon() { 
     return faIcon; 
    } 

    public void setFaIcon(String faIcon) { 
     this.faIcon = faIcon; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getSubject() { 
     return subject; 
    } 

    public void setSubject(String subject) { 
     this.subject = subject; 
    } 
} 

です、あなたはのフィールドを生成したいされて私は推測しますt彼は動的にActionModelクラスを返します。リフレクションを使用してJSON pojoを動的に生成することを参照できます。

関連する問題