1

MSW API指出:

我们的 API 遵循 RESTful 约定。查询位置信息就像调用一样简单:http ://magicseaweed.com/api/YOURAPIKEY/forecast/?spot_id=10

您将收到一个 JSON 数组,其中包含一系列代表特定时间范围内预测的数据,如下所示:

[
{
    "timestamp": 1367366400,
    "localTimestamp": 1367366400,
    "issueTimestamp": 1367366400,
    "fadedRating": 0,
    "solidRating": 0,
    "swell": {
        "minBreakingHeight": 1,
        "absMinBreakingHeight": 0.504,
        "maxBreakingHeight": 1,
        "absMaxBreakingHeight": 0.784,
        "unit": "ft",
        "components": {
            "combined": {
                "height": 1.6,
                "period": 5,
                "direction": 252.47,
                "compassDirection": "ENE"
            },
            "primary": {
                "height": 0.5,
                "period": 7,
                "direction": 92.49,
                "compassDirection": "W"
            },
            "secondary": {
                "height": 0.2,
                "period": 9,
                "direction": 52.9,
                "compassDirection": "SW"
            },
            "tertiary": {
                "height": 1.5,
                "period": 5,
                "direction": 248.62,
                "compassDirection": "ENE"
            }
        }
    },
    "wind": {
        "speed": 6,
        "direction": 221,
        "compassDirection": "NE",
        "chill": 32,
        "gusts": 9,
        "unit": "mph"
    },
    "condition": {
        "pressure": 1026,
        "temperature": 39,
        "weather": "10",
        "unitPressure": "mb",
        "unit": "f"
    },
    "charts": {
        "swell": "htt://chart-1-us.msw.ms/wave/750/1-1367366400-1.gif",
        "period": "htt://chart-1-us.msw.ms/wave/750/1-1367366400-2.gif",
        "wind": "htt://chart-1-us.msw.ms/gfs/750/1-1367366400-4.gif",
        "pressure": "htt://chart-1-us.msw.ms/gfs/750/1-1367366400-3.gif",
        "sst": "htt://chart-1-us.msw.ms/sst/750/1-1367366400-10.gif"
    }
},
{
    "timestamp": 1367377200,
    "localTimestamp": 1367377200,
    "issueTimestamp": 1367366400,
    "fadedRating": 0,
    "solidRating": 0,
    "swell": {
        "minBreakingHeight": 0,
        "absMinBreakingHeight": 0.384,
        "maxBreakingHeight": 1,
        "absMaxBreakingHeight": 0.6,
        "unit": "ft",
        "components": {
            "combined": {
                "height": 1.8,
                "period": 4,
                "direction": 266.06,
                "compassDirection": "E"
            },
            "primary": {
                "height": 0.4,
                "period": 6,
                "direction": 92.53,
                "compassDirection": "W"
            },
            "secondary": {
                "height": 0.2,
                "period": 9,
                "direction": 52.72,
                "compassDirection": "SW"
            },
            "tertiary": {
                "height": 1.8,
                "period": 4,
                "direction": 258.73,
                "compassDirection": "ENE"
            }
        }
    },
    "wind": {
        "speed": 5,
        "direction": 255,
        "compassDirection": "ENE",
        "chill": 34,
        "gusts": 7,
        "unit": "mph"
    },
    "condition": {
        "pressure": 1025,
        "temperature": 41,
        "weather": "10",
        "unitPressure": "mb",
        "unit": "f"
    },
    "charts": {
        "swell": "htt://chart-1-us.msw.ms/wave/750/1-1367377200-1.gif",
        "period": "htt://chart-1-us.msw.ms/wave/750/1-1367377200-2.gif",
        "wind": "htt://chart-1-us.msw.ms/gfs/750/1-1367377200-4.gif",
        "pressure": "htt://chart-1-us.msw.ms/gfs/750/1-1367377200-3.gif",
        "sst": "htt://chart-1-us.msw.ms/sst/750/1-1367377200-10.gif"
    }
},  THIS CONTINUES ON UNTIL THE END OF THE ARRAY

如何调用 JSON 数组以及如何为每个项目分配一个变量(或访问每个项目以便稍后在 html 等中使用)?

谢谢。

4

1 回答 1

1

好的,所以如果您使用 PHP,很容易访问 JSON 数据,只需使用json_decode 函数,您就可以将数据作为PHP 数组访问并轻松使用它。

您的代码应遵循以下任务:

  1. 从 MagicSeaweed API URL 加载 JSON 数据;
  2. 将 JSON 解码为 PHP 数组;
  3. 处理 PHP 数组并检索所需的字段。

在下面的代码中,您只需替换MYAPIKEY为您的 real APIKEY,取消注释该行$json = file_get_contents("magicseaweed.com/api/MYAPIKEY/forecast/?spot_id=10");并注释另一行使用 JSON 子集示例分配$json变量的行。

代码:

<?php
    // You should fetch the JSON data from the remote location the same
    // $json = file_get_contents("magicseaweed.com/api/MYAPIKEY/forecast/?spot_id=10");

    // This is just a subset JSON data for this example
    $json = '[{"timestamp":1367452800,"localTimestamp":1367452800,"issueTimestamp":1367452800,"fadedRating":0,"solidRating":0,"swell":{"minBreakingHeight":0,"absMinBreakingHeight":0,"maxBreakingHeight":0,"absMaxBreakingHeight":0,"unit":"ft","components":{"combined":{"height":2.5,"period":4,"direction":159.26,"compassDirection":"NNW"},"primary":{"height":1.9,"period":4,"direction":158.68,"compassDirection":"NNW"},"secondary":{"height":1.4,"period":5,"direction":255.73,"compassDirection":"ENE"}}},"wind":{"speed":8,"direction":182,"compassDirection":"N","chill":34,"gusts":13,"unit":"mph"},"condition":{"pressure":1024,"temperature":41,"weather":"10","unitPressure":"mb","unit":"f"},"charts":{"swell":"http:\/\/chart-1-us.msw.ms\/wave\/750\/1-1367452800-1.gif","period":"http:\/\/chart-1-us.msw.ms\/wave\/750\/1-1367452800-2.gif","wind":"http:\/\/chart-1-us.msw.ms\/gfs\/750\/1-1367452800-4.gif","pressure":"http:\/\/chart-1-us.msw.ms\/gfs\/750\/1-1367452800-3.gif","sst":"http:\/\/chart-1-us.msw.ms\/sst\/750\/1-1367452800-10.gif"}}]';

    // Decode the JSON data into a PHP array using 'true'
    // as 2nd argument of the json_decode function
    $data = json_decode($json, true);

    // Iterate each of the records and access the data as needed
    foreach ($data as $record) {
        echo "Timestamp: {$record['timestamp']}\n";
        echo "Wind speed: {$record['wind']['speed']}\n";
        echo "Temperature: {$record['condition']['temperature']}\n";
    }

输出:

Timestamp: 1367452800
Wind speed: 8
Temperature: 41
于 2013-05-01T22:40:07.040 回答