簡體   English   中英

從android中json數組內的嵌套json對象獲取數據

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

我試圖從 json 獲取數據,這些數據適用於第一部分,但不適用於嵌套的 json 對象。我想從 json 對象獲取省和城市。

我試圖從 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);
        }
    }
}

當我訪問省字符串時,我的應用程序終止。

                            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);

    /**
     * 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 類插入到您的代碼中

從 city 對象中,您可以直接獲取 Province_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM