TShopping

 找回密碼
 註冊
搜索
查看: 1475|回復: 0
打印 上一主題 下一主題

[分享] 如何在 PHP 中解析 JSON 檔案 及POST接收檔案

[複製鏈接]
跳轉到指定樓層
1#
發表於 2021-9-23 21:57:23 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
示例程式碼中使用的 JSON 檔案的內容如下。

  1. [
  2.     {
  3.         "id": "01",
  4.         "name": "Olivia Mason",
  5.         "designation": "System Architect"
  6.     },
  7.     {
  8.         "id": "02",
  9.         "name": "Jennifer Laurence",
  10.         "designation": "Senior Programmer"
  11.     },
  12.     {
  13.         "id": "03",
  14.         "name": "Medona Oliver",
  15.         "designation": "Office Manager"
  16.     }
  17. ]
複製代碼
如果要處理 JSON POST 請求:
  1. // 從請求中獲取原始數據
  2. $json = file_get_contents('php://input');

  3. // 將其轉換為 PHP 對象
  4. $data = json_decode($json);
複製代碼



使用 file_get_contents() 函式解析 PHP 中的 JSON 檔案
內建函式 file_get_contents() 用於讀取檔案並將其儲存為字串。通過使用此函式,我們可以將 JSON 檔案解析為字串。使用此函式的正確語法如下。

  1. file_get_contents($pathOfFile, $searchPath, $customContext, $startingPoint, $length);
複製代碼

此函式接受五個引數。這些引數的詳細資訊如下。

引數

描述
pathOfFile強制性的它指定檔案的路徑
searchPath可選的它指定搜尋檔案的路徑
customContext可選的它用於指定自定義上下文
startingPoint可選的它指定讀取檔案的起點
length可選的它是要讀取的檔案的最大長度(以位元組為單位)

以下程式顯示瞭如何解析 JSON 檔案。

  1. <?php
  2. $JsonParser = file_get_contents("myfile.json");
  3. var_dump($JsonParser);
  4. ?>
複製代碼

函式 file_get_contents() 僅解析儲存在 JSON 檔案中的 JSON 資料。我們無法直接使用此資料。

輸出:

  1. string(328) "[
  2.     {
  3.         "id": "01",
  4.         "name": "Olivia Mason",
  5.         "designation": "System Architect"
  6.     },
  7.     {
  8.         "id": "02",
  9.         "name": "Jennifer Laurence",
  10.         "designation": "Senior Programmer"
  11.     },
  12.     {
  13.         "id": "03",
  14.         "name": "Medona Oliver",
  15.         "designation": "Office Manager"
  16.     }
  17. ]"
複製代碼

為了使這些資料有用,我們可以使用 json_decode() 將 JSON 字串轉換為陣列。在以下程式中使用此函式。

  1. <?php
  2. $Json = file_get_contents("myfile.json");
  3. // Converts to an array
  4. $myarray = json_decode($Json, true);
  5. var_dump($myarray); // prints array
  6. ?>
複製代碼

輸出:

  1. array(3) {
  2.   [0]=>
  3.   array(3) {
  4.     ["id"]=>
  5.     string(2) "01"
  6.     ["name"]=>
  7.     string(12) "Olivia Mason"
  8.     ["designation"]=>
  9.     string(16) "System Architect"
  10.   }
  11.   [1]=>
  12.   array(3) {
  13.     ["id"]=>
  14.     string(2) "02"
  15.     ["name"]=>
  16.     string(17) "Jennifer Laurence"
  17.     ["designation"]=>
  18.     string(17) "Senior Programmer"
  19.   }
  20.   [2]=>
  21.   array(3) {
  22.     ["id"]=>
  23.     string(2) "03"
  24.     ["name"]=>
  25.     string(13) "Medona Oliver"
  26.     ["designation"]=>
  27.     string(14) "Office Manager"
  28.   }
  29. }
複製代碼
參考文章
https://www.delftstack.com/zh-tw ... a-json-file-in-php/
https://www.geeksforgeeks.org/how-to-receive-json-post-with-php/

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-26 23:19 , Processed in 0.054387 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表