java - 如何循环 JSONOBJECT?
问题描述
我试图从 api 循环一个 JSONOBJECT,但是使用这段代码只给了我第一个值,因为我有一个 cardviewlayout,每次 jsonobjects 被循环,我想得到不同的值,但是使用下面的代码给了我循环但是第一个值保持不变
我试图迭代 jsonobject 值,但是对于每个循环,我保持获取第一部分的相同信息,但不循环并获取不同的值
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, uri, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject1 = response.getJSONObject("teams");
JSONObject jsonObject = jsonObject1.getJSONObject("Match");
Iterator<String> temp = jsonObject.keys();
while (temp.hasNext()){
String key = temp.next();
String date = jsonObject.getString( "Date" );
String league = jsonObject.getString("League");
String stadium = jsonObject.getString("Stadium");
int round = jsonObject.getInt("Round");
String hometeam = jsonObject.getString("HomeTeam");
///// versus photo here//
String awayteam = jsonObject.getString("AwayTeam");
int homescore = jsonObject.getInt("HomeGoals");
String time = jsonObject.getString("Time");
int awayscore = jsonObject.getInt("AwayGoals");
String homedetails = jsonObject.getString("HomeGoalDetails");
arrayList.add(new modeclasslivescore(date, league, stadium, round, hometeam,
R.drawable.vsphoto, awayteam, homescore, time, awayscore, homedetails));
}
adapterlivescore = new adapterlivescore(arrayList);
recyclerView.setAdapter(adapterlivescore);
} catch (JSONException e) {
e.printStackTrace();
解决方案
每次打电话时,您都以错误的方式循环它
String date = jsonObject.getString( "Date" );
你会得到第一个对象
推荐阅读
- actions-on-google - Action 已通过认证测试,但仍无法在 Google Home 应用中找到
- c++ - 为什么表达式 (int) +1e10 不会产生 CSAPP 描述的 -2147483648?
- python - 我如何做到这一点,当我打印 Python 列表时,当我打印列表时,每个项目都没有单引号和括号?
- visual-studio-code - 如何配置 svelte 格式化程序以尊重 HTML 中的换行符?
- javascript - 如何从 Chrome 控制台单击时调用 html 函数?
- c - 如何在C中读取和扫描文件的结构记录
- javascript - 如何动态显示消息 react-router-dom
- javascript - Nightwatch JS 中的异步命令执行
- r - 有没有办法创建一个循环来查找数据集中的特定方法?
- python - 烧瓶如何在扩展模板中获取变量