0

how can use php to parse json format?

now I have this link : https://data.tycg.gov.tw/api/v1/rest/dataset/bed8a800-be39-4750-89a6-324b71f5d5fa

I want to parsing this json

{
  "identifier": "bed8a800-be39-4750-89a6-324b71f5d5fa",
  "categoryCode": [
    "e058256f-df7b-4a28-9b4e-6a70b73a0496"
  ],
  "title": "Taipei",
  "description": "Information。",
  "fieldDescription": "More information",
  "type": "Ori",
  "organization": "Travel",
  "organizationContactName": "Mr.James",
  "organizationContactPhone": "0912345678",
  "organizationContactEmail": "example@example.com",
  "accrualPeriodicity": "Now",
  "temporalCoverageFrom": "",
  "temporalCoverageTo": "",
  "modified": "2021-02-17T02:19:49.000537",
  "spatial": "Taipei",
  "language": "",
  "landingPage": "http://example.com",
  "numberOfData": "155",
  "distribution": [
    {
      "resourceID": "bd906b29-9006-40ed-8bd7-67597c2577fc",
      "resourceDescription": "",
      "format": "JSON",
      "resourceModified": "2021-02-17T02:19:49.000537",
      "downloadURL": "https://data.tycg.gov.tw/opendata/datalist/datasetMeta/download?id=bed8a800-be39-4750-89a6-324b71f5d5fa&rid=bd906b29-9006-40ed-8bd7-67597c2577fc",
      "characterSetCode": "UTF-8"
    }
  ]
}

but, when I use this code, it will get error.

$url="https://data.tycg.gov.tw/api/v1/rest/dataset/bed8a800-be39-4750-89a6-324b71f5d5fa";

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($url, true)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}

1 Answers1

0

Well I think the easiest way is just to use json_decode built in function. Just pass your JSON string to it and if you want it to be converted into associative array set the second parameter of the function to true.

Update:

$json = file_get_contents('https://data.tycg.gov.tw/api/v1/rest/dataset/bed8a800-be39-4750-89a6-324b71f5d5fa');
$jsonArray = json_decode($json, true);
print_r($jsonArray);

just tested it and got this result

array (
  'identifier' => 'bed8a800-be39-4750-89a6-324b71f5d5fa',
  'categoryCode' => 
  array (
    0 => 'e058256f-df7b-4a28-9b4e-6a70b73a0496',
  ),
  'title' => '桃園市景點資料(中)',
  'description' => '提供本市景點之觀光資訊,內容包含景點名稱、簡述、X座標、Y座標等資料。',
  'fieldDescription' => 'InfoId(序號)、TYWebsite(導覽網網址)、Name(名稱)、Toldescribe(簡述)、Add(地址)、Zipcode(郵遞區號)、Opentime(開放時間)、Px(X座標)、Py(Y座標)、Website(網站)、Parkinginfo(停車資訊)、Ticketinfo(費用資訊)、Remarks(旅遊叮嚀)、Tel(電話)、Fax(傳真)、Changetime(更新時間)',
  'type' => '原始資料',
  'organization' => '觀光旅遊局',
  'organizationContactName' => '曹先生',
  'organizationContactPhone' => '03-3322101#6209',
  'organizationContactEmail' => '10023909@mail.tycg.gov.tw',
  'accrualPeriodicity' => '即時',
  'temporalCoverageFrom' => '',
  'temporalCoverageTo' => '',
  'modified' => '2021-02-17T02:19:49.000537',
  'spatial' => '桃園市',
  'language' => '',
  'landingPage' => 'http://travel.tycg.gov.tw/zh-tw/Travel/AttractionList',
  'numberOfData' => '155',
  'distribution' => 
  array (
    0 => 
    array (
      'resourceID' => 'bd906b29-9006-40ed-8bd7-67597c2577fc',
      'resourceDescription' => '',
      'format' => 'JSON',
      'resourceModified' => '2021-02-17T02:19:49.000537',
      'downloadURL' => 'https://data.tycg.gov.tw/opendata/datalist/datasetMeta/download?id=bed8a800-be39-4750-89a6-324b71f5d5fa&rid=bd906b29-9006-40ed-8bd7-67597c2577fc',
      'characterSetCode' => 'UTF-8',
    ),
  ),
)
Zeusarm
  • 1,038
  • 6
  • 14