首页 > 解决方案 > 如何循环 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();

标签: javajson

解决方案


每次打电话时,您都以错误的方式循环它

String date = jsonObject.getString( "Date" );

你会得到第一个对象


推荐阅读