如何使用改造从 JSON 中获取数据?

Posted

技术标签:

【中文标题】如何使用改造从 JSON 中获取数据?【英文标题】:how to fetch data from JSON using retrofit? 【发布时间】:2021-10-17 18:58:10 【问题描述】:

我想获取所有这些数据,包括“记分卡”、“逐球”、“检票口”、“阵容”,但有时“记分卡”和“逐球”为空。如果我从模型类中删除了“记分卡”、“逐球”、“小门”、“阵容”,那么代码工作正常,但我想从“记分卡”、“逐球”、“小门”中获取数据, “阵容”。如何解决这个问题

JSON:

 
"success": 1,
"result": [

  "event_key": "6549",
  "event_date_start": "2021-08-12",
  "event_date_stop": "2021-08-16",
  "event_time": "12:00",
  "event_home_team": "England",
  "home_team_key": "34",
  "event_away_team": "India",
  "away_team_key": "30",
  "event_service_home": "Batting team",
  "event_service_away": "Bowling team",
  "event_home_final_result": "391 (128)",
  "event_away_final_result": "364",
  "event_home_rr": "",
  "event_away_rr": "",
  "event_status": "After day 3",
  "event_status_info": "England lead by 27 runs.",
  "country_name": "World",
  "league_name": "Test Series",
  "league_key": "9846",
  "league_round": "",
  "league_season": "2021",
  "event_live": "1",
  "event_home_team_logo": "https://allsportsapi.com/logo-cricket/34_england.png",
  "event_away_team_logo": "https://allsportsapi.com/logo-cricket/30_india.png",
  "scorecard": 
    "ENG - 1st Innings": [
      
        "innings": "IND - 1st Innings",
        "player": "Sharma R. / Rahul L.",
        "type": "Partnership",
        "status": "",
        "pos": "1",
        "R": "126",
        "B": "263",
        "Min": "182",
        "SR": "47.91"
      ,
      
        "innings": "IND - 1st Innings",
        "player": "Pujara C. / Rahul L.",
        "type": "Partnership",
        "status": "",
        "pos": "2",
        "R": "24",
        "B": "38",
        "Min": "32",
        "SR": "63.16"
      ,
    ]
  ,
  "ball_by_ball": 
    "IND - 1st Innings": [
      
        "innings": "IND - 1st Innings",
        "nr": "127",
        "balls": "W",
        "runs": "0",
        "bowler_to_batsma": "Wood M. to Jadeja R."
      ,
      
        "innings": "IND - 1st Innings",
        "nr": "126",
        "balls": ".|1|.|.|.|W",
        "runs": "1",
        "bowler_to_batsma": "Anderson J. to Jadeja R. & Bumrah J."
      ,
    ],
    "ENG - 1st Innings": [
      
        "innings": "ENG - 1st Innings",
        "nr": "16",
        "balls": ".|.",
        "runs": "0",
        "bowler_to_batsma": "Shami M. to Root J."
      ,
      
        "innings": "ENG - 1st Innings",
        "nr": "15",
        "balls": ".|W|W|.|1|.",
        "runs": "1",
        "bowler_to_batsma": "Siraj M. to Sibley D., Hameed H., Root J. & Burns R."
      ,
    ]
  ,
  "wickets": 
    "IND - 1st Innings": [
      
        "innings": "IND - 1st Innings",
        "fall": "125.6 ov",
        "balwer": "Bumrah J.",
        "batsman": "c Buttler J. b Anderson J.",
        "score": "364/9"
      ,
      
        "innings": "IND - 1st Innings",
        "fall": "126.1 ov",
        "balwer": "Jadeja R.",
        "batsman": "c Anderson J. b Wood M.",
        "score": "364/10"
      
    ],
    "ENG - 1st Innings": [
      
        "innings": "ENG - 1st Innings",
        "fall": "14.2 ov",
        "balwer": "Sibley D.",
        "batsman": "c Rahul L. b Siraj M.",
        "score": "23/1"
      ,
      
        "innings": "ENG - 1st Innings",
        "fall": "14.3 ov",
        "balwer": "Hameed H.",
        "batsman": "b Siraj M.",
        "score": "23/2"
      ,
    ]
  ,
  "extra": [
    
  ],
  "lineups": 
    "home_team": 
      "starting_lineups": [
        
          "player": "Burns R.",
          "player_country": "England"
        ,
        
          "player": "Sibley D.",
          "player_country": "England"
        ,
        
          "player": "Anderson J.",
          "player_country": "England"
        
      ],
      "coaches": [
        
          "coache": "Silverwood C.",
          "coache_country": "England"
        
      ]
    ,
    "away_team": 
      "starting_lineups": [
        
          "player": "Sharma R.",
          "player_country": "India"
        ,
        
          "player": "Rahul L.",
          "player_country": "India"
        ,
        
          "player": "Siraj M.",
          "player_country": "India"
        
      ],
      "coaches": [
        
          "coache": "Shastri R.",
          "coache_country": "India"
        
      ]
    
  
,
 
  "event_key": "6549",
  "event_date_start": "2021-08-12",
  "event_date_stop": "2021-08-16",
  "event_time": "12:00",
  "event_home_team": "Pakistan",
  "home_team_key": "34",
  "event_away_team": "India",
  "away_team_key": "30",
  "event_service_home": "Batting team",
  "event_service_away": "Bowling team",
  "event_home_final_result": "391 (128)",
  "event_away_final_result": "364",
  "event_home_rr": "",
  "event_away_rr": "",
  "event_status": "After day 3",
  "event_status_info": "England lead by 27 runs.",
  "country_name": "World",
  "league_name": "Test Series",
  "league_key": "9846",
  "league_round": "",
  "league_season": "2021",
  "event_live": "1",
  "event_home_team_logo": "https://allsportsapi.com/logo-cricket/34_england.png",
  "event_away_team_logo": "https://allsportsapi.com/logo-cricket/30_india.png",
  "scorecard": 
    "ENG - 1st Innings": [
      
        "innings": "IND - 1st Innings",
        "player": "Sharma R. / Rahul L.",
        "type": "Partnership",
        "status": "",
        "pos": "1",
        "R": "126",
        "B": "263",
        "Min": "182",
        "SR": "47.91"
      ,
      
        "innings": "IND - 1st Innings",
        "player": "Pujara C. / Rahul L.",
        "type": "Partnership",
        "status": "",
        "pos": "2",
        "R": "24",
        "B": "38",
        "Min": "32",
        "SR": "63.16"
      ,
    ],
   "PAK- 1st Innings": [
      
        "innings": "IND - 1st Innings",
        "player": "Sharma R. / Rahul L.",
        "type": "Partnership",
        "status": "",
        "pos": "1",
        "R": "126",
        "B": "263",
        "Min": "182",
        "SR": "47.91"
      ,
      
        "innings": "IND - 1st Innings",
        "player": "Pujara C. / Rahul L.",
        "type": "Partnership",
        "status": "",
        "pos": "2",
        "R": "24",
        "B": "38",
        "Min": "32",
        "SR": "63.16"
      ,
    ]
  ,
  "ball_by_ball": 
    "PAK- 1st Innings": [
      
        "innings": "IND - 1st Innings",
        "nr": "127",
        "balls": "W",
        "runs": "0",
        "bowler_to_batsma": "Wood M. to Jadeja R."
      ,
      
        "innings": "IND - 1st Innings",
        "nr": "126",
        "balls": ".|1|.|.|.|W",
        "runs": "1",
        "bowler_to_batsma": "Anderson J. to Jadeja R. & Bumrah J."
      ,
    ],
    "ENG - 1st Innings": [
      
        "innings": "ENG - 1st Innings",
        "nr": "16",
        "balls": ".|.",
        "runs": "0",
        "bowler_to_batsma": "Shami M. to Root J."
      ,
      
        "innings": "ENG - 1st Innings",
        "nr": "15",
        "balls": ".|W|W|.|1|.",
        "runs": "1",
        "bowler_to_batsma": "Siraj M. to Sibley D., Hameed H., Root J. & Burns R."
      ,
    ]
  ,
  "wickets": 
    "PAK- 1st Innings": [
      
        "innings": "IND - 1st Innings",
        "fall": "125.6 ov",
        "balwer": "Bumrah J.",
        "batsman": "c Buttler J. b Anderson J.",
        "score": "364/9"
      ,
      
        "innings": "IND - 1st Innings",
        "fall": "126.1 ov",
        "balwer": "Jadeja R.",
        "batsman": "c Anderson J. b Wood M.",
        "score": "364/10"
      
    ],
    "ENG - 1st Innings": [
      
        "innings": "ENG - 1st Innings",
        "fall": "14.2 ov",
        "balwer": "Sibley D.",
        "batsman": "c Rahul L. b Siraj M.",
        "score": "23/1"
      ,
      
        "innings": "ENG - 1st Innings",
        "fall": "14.3 ov",
        "balwer": "Hameed H.",
        "batsman": "b Siraj M.",
        "score": "23/2"
      ,
    ]
  ,
  "extra": [
    
  ],
  "lineups": 
    "home_team": 
      "starting_lineups": [
        
          "player": "Burns R.",
          "player_country": "England"
        ,
        
          "player": "Sibley D.",
          "player_country": "England"
        ,
        
          "player": "Anderson J.",
          "player_country": "England"
        
      ],
      "coaches": [
        
          "coache": "Silverwood C.",
          "coache_country": "England"
        
      ]
    ,
    "away_team": 
      "starting_lineups": [
        
          "player": "Sharma R.",
          "player_country": "India"
        ,
        
          "player": "Rahul L.",
          "player_country": "India"
        ,
        
          "player": "Siraj M.",
          "player_country": "India"
        
      ],
      "coaches": [
        
          "coache": "Shastri R.",
          "coache_country": "India"
        
      ]
    
  
,
 
  "event_key": "7086",
  "event_date_start": "2021-08-14",
  "event_date_stop": "2021-08-14",
  "event_time": "12:00",
  "event_home_team": "Essex",
  "home_team_key": "207",
  "event_away_team": "Yorkshire",
  "away_team_key": "209",
  "event_service_home": "Bowling team",
  "event_service_away": "Batting team",
  "event_home_final_result": "317/7 (50)",
  "event_away_final_result": "188/10 (38.4)",
  "event_home_rr": "RR 6.34",
  "event_away_rr": "RR 4.86",
  "event_status": "YOR Innings",
  "event_status_info": "Yorkshire require another 130 runs.",
  "country_name": "United Kingdom",
  "league_name": "One-Day Cup",
  "league_key": "9816",
  "league_round": "Quarter-finals",
  "league_season": "2021",
  "event_live": "1",
  "event_home_team_logo": "https://allsportsapi.com/logo-cricket/207_essex.png",
  "event_away_team_logo": "https://allsportsapi.com/logo-cricket/209_yorkshire.png",
  "scorecard": [
  ],
  "ball_by_ball": [
  ],
  "wickets": [
  ],
  "extra": [
  ],
  "lineups": 
    "home_team": 
      "starting_lineups": [
      ],
      "coaches": [
      ]
    ,
    "away_team": 
      "starting_lineups": [
      ],
      "coaches": [
      ]
    
  

]

这是我的代码:

     APIInterface apiInterface = Retrofit.getRetrofit().create(APIInterface.class);
    Call<LiveMatchDetail> liveMatches = apiInterface.getLiveMatches("API_KEY");
    liveMatches.enqueue(new Callback<LiveMatchDetail>() 
        @Override
        public void onResponse(Call<LiveMatchDetail> call, Response<LiveMatchDetail> response)
        
            Log.e("live ---", "onSuccess: " + response.body());
            Toast.makeText(getActivity(), "Live Success!  " + response.body(), 
  Toast.LENGTH_SHORT).show();

            LiveMatchDetail liveMatches = response.body();
            List<LiveMatchDetail.Result> list = liveMatches.getResult();
 

        @Override
        public void onFailure(Call<LiveMatchDetail> call, Throwable t)
        
            Toast.makeText(getActivity(), "Live Failure!", Toast.LENGTH_SHORT).show();

            Log.e("live ---", "onFailure: " + t.getMessage());
        
    );

而且,我收到了这个错误:

 onFailure: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 47320 path $.result[2].scorecard

模型类链接:

https://drive.google.com/file/d/1-EQPbzqPh6bBT93SyG2ajAVz2SRfR8CQ/view?usp=sharing

【问题讨论】:

请分享您的 LiveMatchDetail 数据类 drive.google.com/file/d/1-EQPbzqPh6bBT93SyG2ajAVz2SRfR8CQ/… 错误消息说它需要一个对象,但它是一个位于特定行号和列号的数组。尝试使用 json lint 并查看您的 json 是否有效。还要确保你的模型类正确匹配结构。 查看我的answer 【参考方案1】:
Call<List<LiveMatchDetail>> liveMatches = apiInterface.getLiveMatches("API_KEY");

试试这个,基本上期望响应为List&lt;LiveMatchDetail&gt; 而不是LiveMatchDetail

【讨论】:

以上是关于如何使用改造从 JSON 中获取数据?的主要内容,如果未能解决你的问题,请参考以下文章

使用改造从 api 获取数据时出错

如何从 php 页面(没有 json)获取数据?

使用改造从(本地)android资产文件夹获取Json数据

如何使用改造 2 在 Android 中获取 Json,如下面的代码所示?

如何使用带有标头的改造来检索 JSON?

改造 2:从响应正文中获取 JSON