使用PHP从JSON API获取数据

时间:2015-11-11 02:57:43

标签: php json

我无法从API中提取数据,我想用于学校项目..

The link to the 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点钟,所以希望有人可以帮助我。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果要提取单元格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>";
}
?>