簡體   English   中英

使用 PHP 從 JSON API 中獲取數據

[英]Getting data out of a JSON API with PHP

我無法從 API 中提取數據,我想用於學校項目。

API 的鏈接 i here或者這里是 API 輸出的示例

{
    "help": "http://www.odaa.dk/api/3/action/help_show?name=datastore_search",
    "success": true,
    "result": {
        "resource_id": "2a82a145-0195-4081-a13c-b0e587e9b89c",
        "fields": [
            {
                "type": "int4",
                "id": "_id"
            },
            {
                "type": "text",
                "id": "date"
            },
            {
                "type": "text",
                "id": "garageCode"
            },
            {
                "type": "int4",
                "id": "totalSpaces"
            },
            {
                "type": "int4",
                "id": "vehicleCount"
            }
        ],
        "records": [
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 62,
                "_id": 1,
                "totalSpaces": 65,
                "garageCode": "NORREPORT"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 512,
                "_id": 2,
                "totalSpaces": 512,
                "garageCode": "SKOLEBAKKEN"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 236,
                "_id": 3,
                "totalSpaces": 1240,
                "garageCode": "SCANDCENTER"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 40,
                "_id": 4,
                "totalSpaces": 953,
                "garageCode": "BRUUNS"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 2932,
                "_id": 5,
                "totalSpaces": 142,
                "garageCode": "BUSGADEHUSET"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 18,
                "_id": 6,
                "totalSpaces": 383,
                "garageCode": "MAGASIN"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 3,
                "_id": 7,
                "totalSpaces": 210,
                "garageCode": "KALKVAERKSVEJ"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 255,
                "_id": 8,
                "totalSpaces": 700,
                "garageCode": "SALLING"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 0,
                "_id": 9,
                "totalSpaces": 0,
                "garageCode": "DOKK1"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 34,
                "_id": 10,
                "totalSpaces": 449,
                "garageCode": "Navitas"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 105,
                "_id": 11,
                "totalSpaces": 105,
                "garageCode": "NewBusgadehuset"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 9,
                "_id": 12,
                "totalSpaces": 319,
                "garageCode": "Urban Level 1"
            },
            {
                "date": "2015/11/11 03:50:07",
                "vehicleCount": 15,
                "_id": 13,
                "totalSpaces": 654,
                "garageCode": "Urban Level 2+3"
            }
        ],
        "_links": {
            "start": "/api/action/datastore_search?resource_id=2a82a145-0195-4081-a13c-b0e587e9b89c",
            "next": "/api/action/datastore_search?offset=100&resource_id=2a82a145-0195-4081-a13c-b0e587e9b89c"
        },
        "total": 13
    }
}

我想提取名為“vehicleCount”和“_id”的單元格,但我似乎不知道該怎么做:(

$json = file_get_contents('http://www.odaa.dk/api/action/datastore_search?resource_id=2a82a145-0195-4081-a13c-b0e587e9b89c');
$json = json_decode($json);
echo $json->fields->_id;

我嘗試了很多不同的方法,但現在失去了想法,而且是凌晨 4 點,所以希望有人能幫助我。

$json->result->records[0]->_id

如果要提取單元格vehicleCount_id

這是我為您提供的解決方案:

<?php 
$json = file_get_contents('http://www.odaa.dk/api/action/datastore_search?resource_id=2a82a145-0195-4081-a13c-b0e587e9b89c');
$json = json_decode($json);


foreach ($json->result->records as $val) {
  echo "_id = " .$val->_id . " vehicleCount = " . $val->vehicleCount . "<br>";
}
?>   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM