Parse Dynamic Nested Array in Android Retrofit

In the next json, I can read the category field . How can I read the effect_list 4 and 1 attributes which are Dynamic ?

     {  
   "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"

     }
    ] 
        } ]  
 }

What have i done so far

ContactList.java

   @SerializedName("categories")
@Expose
private List<Category> categories = null;


@SerializedName("effect_list")
@Expose
private Map<String, List<List<EffectList>>> effectlist;

public Map<String, List<List<EffectList>>> getEffectlist() {
    return effectlist;
}

public void setEffectlist(Map<String, List<List<EffectList>>> effectlist) {
    this.effectlist = effectlist;
}


public List<Category> getCategories() {
    return categories;
}

public void setCategories(List<Category> categories) {
    this.categories = categories;
}

EffectList.java

@SerializedName("effects_id")
@Expose
private String effectsId;

@SerializedName("effects_name")
@Expose
private String effectsName;

public String getEffectsId() {
    return effectsId;
}

public void setEffectsId(String effectsId) {
    this.effectsId = effectsId;
}

public String getEffectsName() {
    return effectsName;
}

public void setEffectsName(String effectsName) {
    this.effectsName = effectsName;
} 

ApiService.java

 @GET("json_new")
Call<ContactList> getMyJSON();

Now I can read the category attributes , but effect_list is NULL. How to read the attributes of the effect list , where 4 and 1 : Dynamic using modification?

Edit:

ArrayAdapter . MyContactAdapter1.java?

MyContactAdapter1.java

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


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

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

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

    EffectList item = getItem(position);



    //   vh.textViewName.setText(item.getEffectsId());

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

    return vh.rootView;
}

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

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

    public static MyContactAdapter1.ViewHolder1 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 MyContactAdapter1.ViewHolder1(rootView, imageView, textViewName, textViewEmail);
    }
}

Main.java

  call.enqueue(new Callback<ContactList>() {
        @Override
        public void onResponse(Call<ContactList> call, Response<ContactList> response) {                 

            if (response.isSuccessful()) {     

                Toast.makeText(getApplicationContext(),"Response Success",Toast.LENGTH_LONG).show();
                Log.d("RESPONSE: ", contactList1.toString());

                contactList = (List<Map<String,List<EffectList>>>) response.body().getEffectlist();
                adapter = new MyContactAdapter1(uploadpage.this, contactList);
                listView.setAdapter(adapter);


            } else {

            }
        }
+4

Source: https://habr.com/ru/post/1664642/


All Articles