当前位置: 首页 > 知识库问答 >
问题:

在java数据模式中映射嵌套json

阮桐
2023-03-14
[
  {
    "couponDate": "2019-07-14T11:34:09.482Z",
    "couponComment": "GooD LucK :)",
    "couponNumber": 1,
    "_id": "5d2b134149db640017f8fda4",
    "overUnder": [
      {
        "betDate": "2019-07-14T11:34:09.472Z",
        "competition": {
          "country": "jh",
          "league": "jhj"
        },
        "homeTeam": "jh",
        "awayTeam": "hj",
        "gameStarted": false,
        "gameFinished": false,
        "homeHalfTimeScore": -1,
        "awayHalfTimeScore": -1,
        "homeScore": -1,
        "awayScore": -1,
        "tip": "Over0.5",
        "risk": 0,
        "tipResult": "pending",
        "betCategory": "overUnder",
        "tipper": "reptile",
        "_id": "5d2b143749db640017f8fda6"
      },
      {
        "betDate": "2019-07-14T13:24:19.804Z",
        "competition": {
          "country": "kenya",
          "league": "add"
        },
        "homeTeam": "da",
        "awayTeam": "ada",
        "gameStarted": false,
        "gameFinished": false,
        "homeHalfTimeScore": -1,
        "awayHalfTimeScore": -1,
        "homeScore": -1,
        "awayScore": -1,
        "tip": "1X",
        "risk": 0,
        "tipResult": "pending",
        "betCategory": "overUnder",
        "tipper": "reptile",
        "_id": "5d2b30043b2bc800172f5f3f"
      }
    ],
    "threeWay": [
      {
        "betDate": "2019-07-14T13:24:19.804Z",
        "competition": {
          "country": "kenya",
          "league": "add"
        },
        "homeTeam": "da",
        "awayTeam": "ada",
        "gameStarted": false,
        "gameFinished": false,
        "homeHalfTimeScore": -1,
        "awayHalfTimeScore": -1,
        "homeScore": -1,
        "awayScore": -1,
        "tip": "1X",
        "risk": 0,
        "tipResult": "pending",
        "betCategory": "threeWay",
        "tipper": "reptile",
        "_id": "5d2b30083b2bc800172f5f40"
      }
    ],
    "doubleChance": [
      {
        "betDate": "2019-07-14T13:24:19.804Z",
        "competition": {
          "country": "kenya",
          "league": "add"
        },
        "homeTeam": "da",
        "awayTeam": "ada",
        "gameStarted": false,
        "gameFinished": false,
        "homeHalfTimeScore": -1,
        "awayHalfTimeScore": -1,
        "homeScore": -1,
        "awayScore": -1,
        "tip": "1X",
        "risk": 0,
        "tipResult": "pending",
        "betCategory": "doubleChance",
        "tipper": "reptile",
        "_id": "5d2b30003b2bc800172f5f3e"
      }
    ],
    "goalGoal": [
      {
        "betDate": "2019-07-14T13:24:19.804Z",
        "competition": {
          "country": "kenya",
          "league": "add"
        },
        "homeTeam": "da",
        "awayTeam": "ada",
        "gameStarted": false,
        "gameFinished": false,
        "homeHalfTimeScore": -1,
        "awayHalfTimeScore": -1,
        "homeScore": -1,
        "awayScore": -1,
        "tip": "1X",
        "risk": 0,
        "tipResult": "pending",
        "betCategory": "goalGoal",
        "tipper": "reptile",
        "_id": "5d2b300c3b2bc800172f5f41"
      }
    ],
    "halfTime": [
      {
        "betDate": "2019-07-14T13:24:19.804Z",
        "competition": {
          "country": "kenya",
          "league": "add"
        },
        "homeTeam": "da",
        "awayTeam": "ada",
        "gameStarted": false,
        "gameFinished": false,
        "homeHalfTimeScore": -1,
        "awayHalfTimeScore": -1,
        "homeScore": -1,
        "awayScore": -1,
        "tip": "1X",
        "risk": 0,
        "tipResult": "pending",
        "betCategory": "halfTime",
        "tipper": "reptile",
        "_id": "5d2b30113b2bc800172f5f42"
      }
    ],
    "bookingPts": [

    ],
    "__v": 0
  }
]
public class gamesmodal {

    String couponComment;

    overs overUnder;
    String threeWay;
    String doubleChance;
    String goalGoal;
    String halfTime;
    String bookingPts;

    public String getTitle() {
       return couponComment;
    }

    public void setTitle(String couponComment) {
        this.couponComment = couponComment;
    }
 public class overs extends gamesmodal {
    String homeTeam;
    String awayTeam;
    String tip;

    public String getHomeTeam() {
        return homeTeam;
    }

    public void setHomeTeam(String homeTeam) {
        this.homeTeam = homeTeam;
    }

    public String getAwayTeam() {
        return awayTeam;
    }

    public void setAwayTeam(String awayTeam) {
        this.awayTeam = awayTeam;
    }

    public String getTip() {
        return tip;
    }

    public void setTip(String tip) {
        this.tip = tip;
    }
}



    public void setOverUnder(overs overUnder) {
        this.overUnder = overUnder;
    }


}
import java.util.List;

public class datamodal {

private List<overUnder> overUnder = null;
String couponComment;
private List<threeWay> threeWay = null;
private List<DoubleChance> doubleChance = null;
private List<HalfTime> halfTime = null;
private List<GoalGoal> goalGoal = null;


public String getCouponComment() {
    return couponComment;
}

public void setCouponComment(String couponComment) {
    this.couponComment = couponComment;
}

public List<overUnder> getOverUnder() {
    return overUnder;
}

public void setOverUnder(List<overUnder> overUnder) {

    this.overUnder = overUnder;
}

public List<DoubleChance> getDoubleChance() {
    return doubleChance;
}

public void setDoubleChance(List<DoubleChance> doubleChance) {
    this.doubleChance = doubleChance;
}

public List<HalfTime> getHalfTime() {
    return halfTime;
}

public void setHalfTime(List<HalfTime> halfTime) {
    this.halfTime = halfTime;
}

public List<GoalGoal> getGoalGoal() {
    return goalGoal;
}

public void setGoalGoal(List<GoalGoal> goalGoal) {
    this.goalGoal = goalGoal;
}

public List<threeWay> getThreeWay() {
    return threeWay;
}

public void setThreeWay(List<threeWay> threeWay) {
    this.threeWay = threeWay;
}

我需要在这里显示json响应

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonArrayRequest;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class matchResult extends Fragment {
    private static final String TAG = matchResult.class.getSimpleName();
    private static final String URL = "my url is here";

    private RecyclerView recyclerView;
    private List<datamodel> movieList;
    private StoreAdapter mAdapter;

    public matchResult() {
        // Required empty public constructor
    }

    public List<datamodel> datamodel() {
        return datamodel();
    }

    public static matchResult newInstance(String param1, String param2) {
        matchResult fragment = new matchResult();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_match_result, container, false);

        recyclerView = view.findViewById(R.id.recycler_view);
        movieList = new ArrayList<>();
        mAdapter = new StoreAdapter(getActivity(), movieList);



        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);
        recyclerView.setNestedScrollingEnabled(false);

        fetchStoreItems();

        return view;
    }

    private void fetchStoreItems() {
        JsonArrayRequest request = new JsonArrayRequest(URL,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        if (response == null) {
                            Toast.makeText(getActivity(), "Couldn't fetch the store items! Pleas try again.", Toast.LENGTH_LONG).show();
                            return;
                        }

                        List<datamodel> items = new Gson().fromJson(response.toString(), new TypeToken<List<datamodel>>() {
                        }.getType());

                        movieList.clear();
                        movieList.addAll(items);

                        // refreshing recycler view
                        mAdapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // error in getting json
                Log.e(TAG, "Error: " + error.getMessage());
                Toast.makeText(getActivity(), "Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        MyApplication.getInstance().addToRequestQueue(request);
    }


    class StoreAdapter extends RecyclerView.Adapter<StoreAdapter.MyViewHolder> {
        private Context context;
        private List<datamodel> movieList;

        public class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView name, price;
            public ImageView thumbnail;

            public MyViewHolder(View view) {
                super(view);
                name = view.findViewById(R.id.title);
                price = view.findViewById(R.id.price);
                thumbnail = view.findViewById(R.id.thumbnail);
            }
        }

        public StoreAdapter(Context context, List<datamodel> movieList) {
            this.context = context;
            this.movieList = movieList;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item, parent, false);

            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, final int position) {
            final datamodel movie = movieList.get(position);

            holder.name.setText((CharSequence) movie.getHomeTeam());
            holder.price.setText((CharSequence) movie.getAwayTeam());
        }

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

共有1个答案

牧梓
2023-03-14
    String threeWay;
    String doubleChance;
    String goalGoal;
    String halfTime;
    String bookingPts
 类似资料:
  • 我不知道如何在Java类中处理嵌套的JSON值。为了尽可能简单,我创建了四个Java类,每个类都有“嵌套级别”。然而,我试图在一个Java类中包含所有这些值。我该怎么做? Json: Java类:

  • 我使用MapStruct来映射我的实体,我使用Mockito来嘲笑我的对象。 我想用MapStruct测试一个包含映射的方法。问题是嵌套映射器在我的单元测试中总是为null(在应用程序中工作得很好)

  • 我用的是Protobuf 3。从文档来看,似乎无法定义嵌套贴图: 我正在尝试创建一种消息类型来表示期权链的定价信息(出价和要价)。对于那些不熟悉这些金融工具的人,基本上我有一套“到期日期(YYYYMMDD)”。在每个过期日期中,我都有一组“strikes(float number;如果需要,可以用字符串表示,我同意)”。在每次行使中,我有两个期权,一个“看跌”和一个“看涨”(这被称为期权的“右”)

  • 我尝试使用MapStruct编写映射器类,如下所示: 目前它显示了“未知属性”“customer.customerid”和“usertypes.usertype.userid”等错误。有人能帮我用MapStruct映射所有这些元素吗? 问题2:我们如何绘制跟踪图?1)customerId usertypes->user->userid 2)pdtPrice offers->OffersType->

  • 我用下面的方法尝试了嵌套映射。 我在声明“root_cause”时出错:[{“type”:“mapper_parsing_exception”,“reason”:“root映射定义有不支持的参数:[type:nested]。” 感谢您的帮助。

  • 我有tje下面的映射器,我使用服务通过代码获取实体