【发布时间】:2019-04-18 21:31:56
【问题描述】:
我试图从 api 循环 A 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();
【问题讨论】:
-
发布您的 json。
-
Android Studio 与这个问题有什么关系?
-
如果您使用 get 直接获取所需的对象,为什么还要循环。
-
这是 json { "teams":{ "Match":{ "Date":"2019-04-18T09:50:00+00:00", "League":"Australian A -League,“Round”:“26”,“HomeTeam”:“Sydney FC”,“HomeTeam_Id”:“134473”,“AwayTeam”:“Perth Glory”,“AwayTeam_Id”:“134481”,“Time”: "Finished", "HomeGoals":"1", "AwayGoals":"0", "HomeGoalDetails":"37':Adam Le Fondre;", "AwayGoalDetails":{
-
Karan Mer,在我使用 for 循环之前使用 jsonarray,我每次通过 jsonobject.getString(i); 得到不同的值;但由于在这个 json 中,我应该循环 JSONOBJECT 值,我只能使用迭代器的第一个值