2016-12-16 5 views
0

下のjsonでは、以下のアンドロイドコードを使用して、「カテゴリ」のフィールドを読み取ることができます。私は"effect_list"要素"4""1"をどのように読むのか分かりません。 「4」および「1」はインクリメンタルであり、ダイナミックです。このためのpojoクラスの作成方法とコードの使用方法Main_get.javaAndroidでの複数のjsonアレイのアップグレードRetrofit 2

   { 
    "categories":[ 
     { 
     "mcategory_id":"4", 
     "mcategory_name":"Band" 

     }, 
     { 
     "mcategory_id":"1", 
     "mcategory_name":"Basic Effects" 

     }, 
     { 
     "mcategory_id":"3", 
     "mcategory_name":"Bg Image Card" 

     } 

    ], 
    "effect_list":{ 
     "4":[ 
     { 
      "effects_id":"18", 
      "effects_name":"Band 1" 

     }, 
     { 
      "effects_id":"19", 
      "effects_name":"Band 2" 

     } 

     ], 
     "1":[ 
     { 
      "effects_id":"1", 
      "effects_name":"Background Blur" 

     }, 
     { 
      "effects_id":"4", 
      "effects_name":"Blemish Removal" 

     } 
     ] 
    } 
} 

私は、JSON配列「カテゴリ」使用して改造し、問題なくその作業を取得するには、次のコードを使用しています。では、どうすれば"effect_list"フィールドを取得できますか?

Contact.java

public class Contact { 


    @SerializedName("mcategory_id") 
    @Expose 
    private String mcategory_id; 


    @SerializedName("mcategory_name") 
    @Expose 
    private String mcategory_name; 


    public String getmcategory_id() { 
     return name; 
    } 


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


    public String getmcategory_name() { 
     return email; 
    } 


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

} 

ApiService.java

 public interface ApiService { 

    @GET("xyz.json") 
    Call<ContactList> getMyJSON(); 
    } 

ContactList.java

  public class ContactList { 

     @SerializedName("contacts") 
     @Expose 
     public ArrayList<Contact> contacts = new ArrayList<>(); 

/** 
* @return The contacts 
*/ 
     public ArrayList<Contact> getContacts() { 
     return contacts; 
    } 

/** 
* @param contacts The contacts 
*/ 
    public void setContacts(ArrayList<Contact> contacts) { 
     this.contacts = contacts; 
    } 
} 

RetroClient.java

 public class RetroClient { 


    private static final String ROOT_URL = "http://abc.ab/"; 


    private static Retrofit getRetrofitInstance() { 
    return new Retrofit.Builder() 
      .baseUrl(ROOT_URL) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 
    } 

    /** 
    * Get API Service 
    * 
    * @return API Service 
    */ 
    public static ApiService getApiService() { 
     return getRetrofitInstance().create(ApiService.class); 
    } 
} 

MyContactAdapter.java

  List<Contact> contactList; 
    Context context; 
    private LayoutInflater mInflater; 


    // Constructors 
    public MyContactAdapter(Context context, List<Contact> objects) { 
     super(context, 0, objects); 
     this.context = context; 
     this.mInflater = LayoutInflater.from(context); 
     contactList = objects; 
    } 

    @Override 
    public Contact getItem(int position) { 
     return contactList.get(position); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final ViewHolder vh; 
     if (convertView == null) { 
      View view = mInflater.inflate(R.layout.get_layout_row_view,    parent, false); 
     vh = ViewHolder.create((RelativeLayout) view); 
     view.setTag(vh); 
    } else { 
     vh = (ViewHolder) convertView.getTag(); 
    } 

    Contact item = getItem(position); 


    vh.textViewName.setText(item.getName()); 
    vh.textViewEmail.setText(item.getEmail()); 
    // Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView); 
    Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView); 

    // loading.dismiss(); 

    return vh.rootView; 
} 

public static class ViewHolder { 
    public final RelativeLayout rootView; 
    public final ImageView imageView; 
    public final TextView textViewName; 
    public final TextView textViewEmail; 

    public ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) { 
     this.rootView = rootView; 
     this.imageView = imageView; 
     this.textViewName = textViewName; 
     this.textViewEmail = textViewEmail; 
    } 

    public static ViewHolder create(RelativeLayout rootView) { 
     ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView); 
     TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName); 
     TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail); 
     return new ViewHolder(rootView, imageView, textViewName, textViewEmail); 
    } 
} 

Main_get.java

  public class Main_get extends AppCompatActivity { 

    /** 
    * Views 
    */ 
    private GridView listView; 
    private View parentView; 

    private ArrayList<Contact> contactList; 
    private MyContactAdapter adapter; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    /** 
    * Array List for Binding Data from JSON to this List 
    */ 
    contactList = new ArrayList<>(); 

    parentView = findViewById(R.id.parentLayout); 

    /** 
    * Getting List and Setting List Adapter 
    */ 
    listView = (GridView) findViewById(R.id.listView); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int  position, long id) { 
      Snackbar.make(parentView, contactList.get(position).getName() + " => " + contactList.get(position).getName().getBytes(), Snackbar.LENGTH_LONG).show(); 
     } 
    }); 


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    assert fab != null; 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(@NonNull final View view) { 





      //Creating an object of our api interface 
      ApiService api = RetroClient.getApiService(); 

      /** 
      * Calling JSON 
      */ 
      Call<ContactList> call = api.getMyJSON(); 

      /** 
      * Enqueue Callback will be call when get response... 
      */ 
      call.enqueue(new Callback<ContactList>() { 
       @Override 
       public void onResponse(Call<ContactList> call,  Response<ContactList> response) { 


        if(response.isSuccessful()) { 
         /** 
         * Got Successfully 
         */ 
         contactList = response.body().getContacts(); 

         /** 
         * Binding that List to Adapter 
         */ 
         adapter = new MyContactAdapter(Main_get.this, contactList); 
         listView.setAdapter(adapter); 

        } else { 
         // Snackbar.make(parentView,  R.string.string_some_thing_wrong, Snackbar.LENGTH_LONG).show(); 
        } 
       } 

       @Override 
       public void onFailure(Call<ContactList> call, Throwable t) { 
        dialog.dismiss(); 
       } 
      }); 

     } 


     }); 
    } 
} 

私はこのコードで行う必要があり、すべての変更は何ですか?

+0

Jsonのように、JsonObject内にDynamic JsonObjectsを配置しないでください。 Dynamic JsonObjectsはJsonArray内で使用する必要があります。 –

+0

私はあなたがjsonをより理解しやすいものに修正すべきだと思います。あなたは、連絡先オブジェクト –

答えて

0

あなたは、インデックスを取得します(「1」、「4」...)とリストにあなたが効果の配列を取得します整数で

Map<Integer, List<Effect>> 

を使用することができます。

+1

のおかげで、その効果を置くことができます。私は** Main_get.java **で何をすべきですか? – Jayaprakash

関連する問題