【问题标题】:Getting JSON data from API as array or object从 API 获取 JSON 数据作为数组或对象
【发布时间】:2018-09-13 12:28:06
【问题描述】:

我正在使用sports-radar 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 团队名称时,我收到错误 Illegal string offset '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, ),

【问题讨论】:

  • 只执行此操作$schedule = json_decode($jsonData, true);,现在检查var_export($schedule); 以检查away 存在于哪个索引处,然后尝试调整foreach() 代码
  • 好的,给我一秒钟检查一下,我会回复你的。谢谢
  • 做一个var_dump($someArray)...让我们看看你有什么。
  • 您不能将 $value->home 和 $value->away 转换为字符串,因为它们都是对象。
  • 您的代码已将$someObject = jsonObject 更正为$someObject = $jsonObject

标签: php json api


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多