POJO类如何在JSON Objec中获取嵌套的JSON对象

时间:2017-04-27 23:45:15

标签: java json pojo

我正在尝试访问嵌套在另一个Json对象中的Json对象。我写了一个POJO类,但不允许我访问数据:

这是Json示例:

   {
            "relationships": {
                "competition": {
                    "data": {
                        "type": "competitions",
                        "id": "37"
                    }
                }
            },
            "attributes": {
                "has_league_table": false,
                "name": "Round of 16",
                "stage": "Round of 16"
            },
            "type": "rounds",
            "id": "881"
        },

我想访问Relationships中的数据 JSON对象 - >比赛JSON对象

这是我的POJO课程:

public class Relationships {

    private Competition competition;

    public Competition getCompetition ()
    {
        return competition;
    }

    public void setCompetition (Competition competition)
    {
        this.competition = competition;
    }


}

比赛POJO

public class Competition {

    private Data data;

    public Data getData ()
    {
        return this.data;
    }

    public void setData (Data data)
    {
        this.data = data;
    }

}

DATA POJO

public class Data {

    private String id;

    private String type;

    public String getId ()
    {
        return this.id;
    }

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

    public String getType ()
    {
        return this.type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

}

我的适配器

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{

    private List<Included> includedData;


    public Adapter(List<Included> includedData) {
        this.includedData = includedData;

    }


    @Override
    public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {


        holder.tvCompetitionName.setText(includedData.get(position).getAttributes().getName());
        holder.homeTeam.setText(includedData.get(position).getRelationships().getCompetition().getData().getType());



        holder.itemView.setTag(includedData.get(position));

    }

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

    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvCompetitionName;
        private TextView homeTeam;
        private TextView score;

        public ViewHolder(View itemView) {
            super(itemView);

            tvCompetitionName = (TextView) itemView.findViewById(R.id.competitionNameTV);
            homeTeam = (TextView) itemView.findViewById(R.id.homeTeamTv);
            score = (TextView) itemView.findViewById(R.id.score);
        }
    }
}

但是在我的onBindViewHolder

holder.homeTeam.setText(includedData.get(位置).getRelationships()getCompetition()的getData()的getType());

产生错误:

java.lang.NullPointerException:尝试调用虚方法&#39; com.xxx.xxxx.DataModel_Included.Data com.xxxx.xxxx.DataModel_Included.Competition.getData()&#39;在空对象引用上

对此有何帮助?

0 个答案:

没有答案