从API获取JSON数据作为数组或对象

时间:2018-09-04 05:19:26

标签: php json api

我正在使用体育雷达API来获取NFL第1周的时间表。API以json格式(例如,我缩短了字符串)返回以下数据。

"id": "8e45fe2d-fb95-4504-845d-7c815623ccd6",
    "year": 2018,
    "type": "REG",
    "name": "REG",
    "week": {
        "id": "37435167-5cf6-4cce-b405-ff0e264ced9c",
        "sequence": 1,
        "title": "1",
        "games": [{
    "id": "0822b924-eadc-4398-bfe6-83cbbf3a2912",
            "status": "scheduled",
            "reference": "57570",
            "number": 4,
            "scheduled": "2018-09-09T17:00:00+00:00",
            "entry_mode": "INGEST",
            "venue": {
        "id": "6ed18563-53e0-46c2-a91d-12d73a16456d",
                "name": "Lucas Oil Stadium",
                "city": "Indianapolis",
                "state": "IN",
                "country": "USA",
                "zip": "46225",
                "address": "500 South Capitol Avenue",
                "capacity": 67000,
                "surface": "artificial",
                "roof_type": "retractable_dome"
            },
            "home": {
        "id": "82cf9565-6eb9-4f01-bdbd-5aa0d472fcd9",
                "name": "Indianapolis Colts",
                "alias": "IND",
                "game_number": 1
            },
            "away": {
        "id": "ad4ae08f-d808-42d5-a1e6-e9bc4e34d123",
                "name": "Cincinnati Bengals",
                "alias": "CIN",
                "game_number": 1
            },
            "broadcast": {
        "network": "CBS"
            }
        }, {
    "id": "0a456149-c547-4856-9b1b-86e1d93887ae",
            "status": "scheduled",
            "reference": "57574",
            "number": 8,
            "scheduled": "2018-09-09T17:00:00+00:00",
            "entry_mode": "INGEST",
            "venue": {
        "id": "3c85d89a-ec66-4983-acd5-1381d6c8673a",
                "name": "Mercedes-Benz Superdome",
                "city": "New Orleans",
                "state": "LA",
                "country": "USA",
                "zip": "70112",
                "address": "1500 Sugar Bowl Drive",
                "capacity": 73208,
                "surface": "artificial",
                "roof_type": "dome"
            },
            "home": {
        "id": "0d855753-ea21-4953-89f9-0e20aff9eb73",
                "name": "New Orleans Saints",
                "alias": "NO",
                "game_number": 1
            },
            "away": {
        "id": "4254d319-1bc7-4f81-b4ab-b5e6f3402b69",
                "name": "Tampa Bay Buccaneers",
                "alias": "TB",
                "game_number": 1
            },
            "broadcast": {
        "network": "FOX"

I used the following website作为有关如何仅显示我需要的数据以及如何遍历它们的教程

请注意,JSON字符串存储在变量$schedule

我的代码

   // JSON string
      $jsonData = $schedule; //get json string

      // Convert JSON string to Array
      $jsonArray = json_decode($jsonData, true);


      // Convert JSON string to Object
      $jsonObject = json_decode($schedule);

遍历PHP数组或对象

$someArray = $jsonArray
  foreach ($someArray as $key => $value) {
    echo $value["home"] . ", " . $value["away"] . "<br>";
  }

  // Loop through Object
  $someObject = jsonObject
  foreach($someObject as $key => $value) {
    echo $value->home . ", " . $value->away . "<br>";
  }

我的错误

当尝试将字符串转换为数组并尝试获取away团队名称时,出现错误非法字符串偏移home相同的问题,并且所有其他数据

尝试将数据作为对象访问时,出现以下错误Trying to get property of non-object

我按照教程讲了这封信。但是我得到上面的基本错误...吗?任何帮助和解释将不胜感激。谢谢

编辑:

var_export($schedule)返回以下内容:

array ( 'id' => '8e45fe2d-fb95-4504-845d-7c815623ccd6', 'year' => 2018, 'type' => 'REG', 'name' => 'REG', 'week' => array ( 'id' => '37435167-5cf6-4cce-b405-ff0e264ced9c', 'sequence' => 1, 'title' => '1', 'games' => array ( 0 => array ( 'id' => '0822b924-eadc-4398-bfe6-83cbbf3a2912', 'status' => 'scheduled', 'reference' => '57570', 'number' => 4, 'scheduled' => '2018-09-09T17:00:00+00:00', 'entry_mode' => 'INGEST', 'venue' => array ( 'id' => '6ed18563-53e0-46c2-a91d-12d73a16456d', 'name' => 'Lucas Oil Stadium', 'city' => 'Indianapolis', 'state' => 'IN', 'country' => 'USA', 'zip' => '46225', 'address' => '500 South Capitol Avenue', 'capacity' => 67000, 'surface' => 'artificial', 'roof_type' => 'retractable_dome', ), 'home' => array ( 'id' => '82cf9565-6eb9-4f01-bdbd-5aa0d472fcd9', 'name' => 'Indianapolis Colts', 'alias' => 'IND', 'game_number' => 1, ),

1 个答案:

答案 0 :(得分:1)

您正在处理数组数组。尝试这样的事情:

  $someArray = $jsonArray
  foreach ($someArray as $key => $value) {
    echo $value["home"]["name"] . ", " . $value["away"]["name"] . "<br>";
  }