Json中的嵌套对象

时间:2017-04-10 16:09:53

标签: android json

这是我的JSON:

Json

我需要访问extra_services并获取service_name

我知道我可以直接使用Gson进行操作,但问题是我需要使用getter和setter,因为我在回收器中使用了一个适配器,我该怎么做?

这是我的适配器类,我需要获取服务名称

public class ExtraServicesAdapter extends RecyclerView.Adapter<ExtraServicesAdapter.ViewHolder> implements View.OnClickListener
{
    private ArrayList<Business> businessList;
    private Activity activity;
    private int layoutMolde,idb;

    public ExtraServicesAdapter(Activity activity, ArrayList<Business> list, int layout)
    {
        this.activity = activity;
        this.businessList = list;
        layoutMolde = layout;
    }

    @Override
    public ExtraServicesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_services_basic, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position)
    {
       if(businessList.get(position).getExtra_services()==null)
       {
           holder.txtNameServiceBasic.setText("There's nothing to show");
       }
        holder.txtNameServiceBasic.setText(businessList.get(position).getExtra_services());
    }

    @Override
    public int getItemCount()
    {
        return businessList.size();
    }

    @Override
    public void onClick(View v)
    {

    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {

        public TextView txtNameServiceBasic;

        public ViewHolder( View itemView)
        {
            super(itemView);
            txtNameServiceBasic = (TextView) itemView.findViewById(R.id.txtNameServiceBasic);
        }
    }
}

这是我的班级,我正在使用的是吸气剂和制定者

public class Business {

    private Integer id,rating;
    private String name, description, cover_url_string, logo_url_string, icon_default,business_name,cover_default,extra_services;
    private Boolean status;

    public Business(){}

    public Business(Integer id,Integer rating,String business_name, String name, String description, String logo_url_string, String cover_default, String icon_default,String cover_url_string,String extra_services) {

        this.id = id;
        this.name = name;
        this.business_name=business_name;
        this.description = description;
        this.logo_url_string = logo_url_string;
        this.cover_url_string = cover_url_string;
        this.rating=rating;
        this.icon_default=icon_default;
        this.cover_default=cover_default;
        this.extra_services=extra_services;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getRating() {
        return rating;
    }

    public String getBusiness_name() {
        return business_name;
    }

    public void setBusiness_name(String business_name) {
        this.business_name = business_name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

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

    public String getLogo_url_string() {
        return logo_url_string;
    }

    public void setLogo_url_string(String logo_url_string) {
        this.logo_url_string = logo_url_string;
    }

     public String getIcon_default() {

        return icon_default;
    }

    public String getCover_default() {
        return cover_default;
    }

   public String getCover_url_string() {
       return cover_url_string;
   }

   public String getExtra_services() {
       return  extra_services;
   }

   public void setExtra_services() {
        this.extra_services=extra_services;
   }
}

1 个答案:

答案 0 :(得分:1)

您可以添加包含ExtraServices

列表的ExtraService

<强> ExtraService

public class ExtraService {
private String Id;
private String ServiceName;

public String getId() {
    return Id;
}

public void setId(String Id) {
    this.Id = Id;
}

public String getServiceName() {
    return ServiceName;
}

public void setServiceName(String ServiceName) {
    this.ServiceName = ServiceName;
}

<强> ExtraServices

public class ExtraServices {
private List<ExtraService> extraServicesList;

public List<ExtraService> getExtraServicesList() {
    return extraServicesList;
}

public void setExtraServicesList(List<ExtraService> extraServicesList) {
    this.extraServicesList = extraServicesList;
}    

public void add(ExtraService extraService){
    if(extraServicesList == null){
        extraServicesList = new ArrayList<>();
    }
    extraServicesList.add(extraService);
}

Business课程中添加ExtraServices

的getter和setter
private ExtraServices extraServices;

public ExtraServices getExtraServices () {
    return extraServices;
}

public void setExtraServices (ExtraServices extraServices) {
    this.extraServices = extraServices;
}

必须执行setter过程并在适配器中执行以下操作后:

holder.txtNameServiceBasic.setText(businessList.get(position).getExtraServices().getExtraServicesList().get(posistion).getServiceName());