如何循环一个JSONOBJECT?

时间:2019-04-18 12:20:59

标签: java json

我试图从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();

3 个答案:

答案 0 :(得分:0)

只需使用foreach循环提取该数据:

您的学习将成为

for (int i : jsonObject) { /*I passed your variables small please edit and put proper one*/
            Log.e("Date", jsonObject.get(i).Date.toString);
            jsonObject.get(i).league;
            jsonObject.get(i).stadium;
            jsonObject.get(i).round;
            jsonObject.get(i).hometeam;
            jsonObject.get(i).Time;
            jsonObject.get(i).AwayTeam;
            jsonObject.get(i).HomeGoalDetails
        }

只是不要使用变量来存储它。您可以登录。他们就像我为第一个展示的一样。

答案 1 :(得分:0)

每次调用

时,循环方式都不正确
String date = jsonObject.getString( "Date" );

您将得到第一个对象

答案 2 :(得分:0)

您将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;"
      },
{
        "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;"
      },
{
        "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;"
      }
    ]
  }
}

这应该是您的代码:

/*Create Your List Object*/

    for (int i : jsonObject) { 
                 jsonObject.get(i).getString("Date");
                jsonObject.get(i).getString("league");
                jsonObject.get(i).getString("stadium");
                jsonObject.get(i).getString("round");
                jsonObject.get(i).getString("hometeam");
                jsonObject.get(i).getString("Time");
                jsonObject.get(i).getString("AwayTeam");
                jsonObject.get(i).getString("HomeGoalDetails");
    /*Add object to List*/
            }