简体   繁体   English

从android中json数组内的嵌套json对象获取数据

[英]Get data from nested json object inside json array in android

I am trying to get data from json that work for first portion but not for nested json object.I want to get the province and city from json object.我试图从 json 获取数据,这些数据适用于第一部分,但不适用于嵌套的 json 对象。我想从 json 对象获取省和城市。

I am trying to get data from json that work for first portion but not for nested json object.I want to get the province and city from json object.我试图从 json 获取数据,这些数据适用于第一部分,但不适用于嵌套的 json 对象。我想从 json 对象获取省和城市。

In province object i want province string and in city i want city string

........................MY JSON.................
{
  "ads": [
    {
      "id": 8,
      "title": "Software Engineering",
      "description": "A book for software engineers",
      "price": 100,
      "user_id": 1,
      "province_id": 4,
      "city_id": 5,
      "subject_id": 1,
      "level_id": 3,
      "active": 1,
      "created_at": "2019-04-20 04:35:00",
      "updated_at": "2019-04-20 04:35:00",
      "province": {
        "id": 4,
        "province": "Blochistan",
        "created_at": null,
        "updated_at": null
      },
      "city": {
        "id": 5,
        "city": "Quetta",
        "province_id": 4,
        "created_at": null,
        "updated_at": null
      }
    }
  ]
}
public class AdsAdapter extends RecyclerView.Adapter<AdsAdapter.CustomViewHolder> {
    private List<Data> adsList;
    private Context context;


    public AdsAdapter(List<Data> dataList,Context context) {
        this.adsList=dataList;
        this.context=context;

    }

    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.ads_list, viewGroup, false);

        return new CustomViewHolder(itemView,adsList,context);
    }

    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder customViewHolder, int i) {
        Data data = adsList.get(i);
        customViewHolder.ad_title.setText(data.getTitle());
        customViewHolder.ad_price.setText(data.getPrice().toString());
//        customViewHolder.ad_city.setText(();
    }
    @Override
    public int getItemCount() {
        return adsList.size();
    }

    public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView ad_title,ad_price,ad_province,ad_city;
        private List<Data> adsList;
        private Context context;
        public CustomViewHolder(@NonNull View itemView,List<Data> adsList,Context context) {
            super(itemView);
            this.adsList=adsList;
            this.context=context;
            itemView.setOnClickListener(this);
            ad_title = (TextView)itemView.findViewById(R.id.current_page);
            ad_price = itemView.findViewById(R.id.ad_price);
            ad_province = itemView.findViewById(R.id.province);
            ad_city = itemView.findViewById(R.id.city);
        }
        @Override
        public void onClick(View v) {
            int position=getAdapterPosition();
            Data data = this.adsList.get(position);
            Intent intent = new Intent(this.context,AdDetail.class);
            intent.putExtra("title",data.getTitle());
            intent.putExtra("discrip",data.getDescription());
            intent.putExtra("price",data.getPrice().toString());
            intent.putExtra("create",data.getCreatedAt().toString());
            this.context.startActivity(intent);
        }
    }
}

my application terminated when i access province string.当我访问省字符串时,我的应用程序终止。

                            Model Class
public class Province {
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("province")
    @Expose
    private String province;
    @SerializedName("created_at")
    @Expose
    private Object createdAt;
    @SerializedName("updated_at")
    @Expose
    private Object updatedAt;

    public Integer getId() {
        return id;
    }

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

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public Object getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Object createdAt) {
        this.createdAt = createdAt;
    }

    public Object getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Object updatedAt) {
        this.updatedAt = updatedAt;
    }
}

Call call = api.getAds(parse_subject_id,parse_level_id); call call = api.getAds(parse_subject_id,parse_level_id);

    /**
     * Enqueue Callback will be call when get response...
     */
    call.enqueue(new Callback<SubjectList>() {
        @Override
        public void onResponse(Call<SubjectList> call, Response<SubjectList> response) {
            pDialog.dismiss();
            if (response.isSuccessful()) {
                /**
                 * Got Successfully
                 */
                adsList = response.body().getAds();
                recyclerView = (RecyclerView) findViewById(R.id.recycler_view_Ads);
                adsAdapter = new AdsAdapter(adsList,getApplicationContext());
                RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(getApplicationContext());
                recyclerView.setLayoutManager(eLayoutManager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setAdapter(adsAdapter);
                /////////////////////////////////////
            }
        }
        @Override
        public void onFailure(Call<SubjectList> call, Throwable t) {
            pDialog.dismiss();
        }
    });
hare i am calling the adapter and pass parameters

使用此工具生成 POJO。您可以直接将您的 POJO 类插入到您的代码中

From city object you can directly get province_id从 city 对象中,您可以直接获取 Province_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM