【问题标题】:Decoding multiple JSON responses using PHP使用 PHP 解码多个 JSON 响应
【发布时间】:2016-07-31 21:40:37
【问题描述】:

我无法使用 json_decode 将此 JSON 响应解码为 PHP,感谢您的帮助:

来自 API 的 JSON 响应

{
      "fixtures": [
        {
          "id": 59757,
          "home_team_id": 24,
          "away_team_id": 18,
          "home_score_penalties": 0,
          "away_score_penalties": 0,
              "formation": {
               "home": null,
               "away": null
              },
          "date_time_tba": false,
          "spectators": null,
          "round_id": 4839
        }
      ]
    }

PHP:

$url = "https://api....";
$data = json_decode(file_get_contents($url), true);
echo $data['fixtures'][0]['home_team_id'];
echo $data['formation'][0]['home'];

我没有得到任何结果!

谢谢。

【问题讨论】:

  • 您的问题不完整。 multiple 是什么意思?实际代码在哪里? 我解码失败...是什么意思?
  • 如果你运行echo '<pre>'; print_r($data); echo '</pre>';你可以看到数组的结构。您可以像这样访问第一个 ID:echo $data['fixtures'][0]['id'];。您可以使用循环来显示值,但正如我所说,您的问题不是很清楚。
  • 我看不出这个 JSON 是如何有效的,对于初学者来说,"round_id": 4839, 不能以逗号结尾。
  • @Basheer 听诺登海姆!还要通过 var_dump(file_get_contents($url)); 检查是否正在下载 json
  • 问题是您的 JSON 可能无效。您可以使用验证器吗?如果var_dump 返回null,则您的 JSON 100% 无效。

标签: php json api


【解决方案1】:

那里有无数的 JSON 验证器。在使用json_decode之前使用它们中的任何一个。

{
    "fixtures": [{
        "id": 59757,
        "home_team_id": 24,
        "away_team_id": 18,
        "home_score_penalties": 0,
        "away_score_penalties": 0,
        "formation": {
            "home": null,
            "away": null
        },
        "date_time_tba": false,
        "spectators": null,
        "round_id": 4839,
    }]
}

您的示例不是有效的 JSON,因为 "round_id": 4839, 最后不能有逗号。有效的 JSON 是:

{
    "fixtures": [{
        "id": 59757,
        "home_team_id": 24,
        "away_team_id": 18,
        "home_score_penalties": 0,
        "away_score_penalties": 0,
        "formation": {
            "home": null,
            "away": null
        },
        "date_time_tba": false,
        "spectators": null,
        "round_id": 4839
    }]
}

现在这个 JSON 在解码后返回一个数组:

$json = '{"fixtures": [{"id": 59757,"home_team_id": 24,"away_team_id": 18,"home_score_penalties": 0,"away_score_penalties": 0,"formation": {"home": null,"away": null},"date_time_tba": false,"spectators": null,"round_id": 4839}]}';

var_dump(json_decode($json, true));

结果:

array(1) {
  ["fixtures"]=>
  array(1) {
    [0]=>
    array(9) {
      ["id"]=>
      int(59757)
      ["home_team_id"]=>
      int(24)
      ["away_team_id"]=>
      int(18)
      ["home_score_penalties"]=>
      int(0)
      ["away_score_penalties"]=>
      int(0)
      ["formation"]=>
      array(2) {
        ["home"]=>
        NULL
        ["away"]=>
        NULL
      }
      ["date_time_tba"]=>
      bool(false)
      ["spectators"]=>
      NULL
      ["round_id"]=>
      int(4839)
    }
  }
}

【讨论】:

【解决方案2】:

首先检查您是否从var_dump(file_get_contents($url)) 接收输出。

然后验证返回的字符串是有效的 JSON。由 Nordenheim 解释 here

确认其有效后,检查 JSON 解码数据以了解如何访问正确的值。

var_dump(json_decode(file_get_contents($url)));

所以以下内容应该是您要寻找的;

$data = json_decode(file_get_contents($url)); echo $data['data'][0]['id'];;

【讨论】:

  • 现在数据使用 JSONLint 验证器有效,但我仍然得到带有 echo $data['fixtures']['home_team_id']; 的空白页面;
  • 您可以将最后一个 var_dump 的内容粘贴到您的问题中吗?
  • 这里是浏览器的前 3 行:{"data":[{"id":629345,"ht_score":"0-0","ft_score":"2-0","et_score":null,"home_team_id":1266,"away_team_id":1275,"home_score":2,"away_score":0,"home_score_penalties":0,"away_score_penalties":0,"formation":{"home":null,"away":null},"date_time_tba":0,"spectators":null,"starting_date":"2016-07-31","starting_time":"13:30:00","status":"FT","minute":90,"extra_minute":0,"competition_id":114,"venue_id":null,"season_id":511,"round_id":7292,"stage_id":873,"aggregate":null,"placeholder":false},
  • using $data = json_decode(file_get_contents($url)); echo $data['data'][0]['id']; 我得到了这个错误: PHP 致命错误:不能使用 stdClass 类型的对象作为 /home/ 中的数组...
  • 尝试将输出转换为数组而不是对象; $data = json_decode(file_get_contents($url), true); echo $data['data'][0]['id'];
猜你喜欢
  • 2013-07-27
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多