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 Array を呼び出す方法と、各項目に変数を割り当てる方法 (または後で 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 に答える