从Json获取ArrayList

She*_*Din -1 java android json

我有来自 REST API 的 Json 响应,如下所示:

{
  "data": {
    "leagues": [
      {
        "identifier": "8e7fa444c4b60383727fb61fcc6aa387",
        "league_slug": "bundesliga",
        "name": "Bundesliga",
        "nation": "Germany",
        "level": "1",
        "federation": "UEFA"
      },
      {
        "identifier": "726a53a8c50d6c7a66fe0ab16bdf9bb1",
        "league_slug": "premier-league",
        "name": "Premier League",
        "nation": "England",
        "level": "1",
        "federation": "UEFA"
      },
      {
        "identifier": "bc425f51d5ee924580c35c38da138de8",
        "league_slug": "serie-a",
        "name": "Serie A",
        "nation": "Italy",
        "level": "1",
        "federation": "UEFA"
      }
    ],
    "statusCode": "200",
    "errorCode": "0",
    "statusReason": "OK"
  }
}
Run Code Online (Sandbox Code Playgroud)

我想从此 Json 获取联赛列表,现在我有“数据”哈希图,但无法获取联赛哈希图数组。我不想将其转换为任何数据类型,我只想ArrayList<Map<String, String>>

Gson请帮助我使用或获取此信息JSONObject

Vya*_*lav 5

您可以使用解析此字符串

JSONObject json = new JSONObject(str); 
Run Code Online (Sandbox Code Playgroud)

当您可以检索所需的任何数据时:

JSONObject data = json.getJSONObject("data");
JSONArray leagues = data.getJSONArray("leagues");
Run Code Online (Sandbox Code Playgroud)

像这样迭代:

for (int i = 0; i < leagues.length(); i++) {
     JSONObject childJSONObject = leagues.getJSONObject(i);
}
Run Code Online (Sandbox Code Playgroud)