JSON 代表 JavaScript 物件表示法。它是一種輕量級資料格式,用於在系統之間儲存和交換訊息,尤其是在 Web 應用程式中。
將 JSON 視為一種以清晰、結構化的格式編寫和組織資料的方法。
{ "name": "Alice", "age": 25, "isStudent": false, "skills": ["JavaScript", "Python", "HTML"], "address": { "street": "123 Main St", "city": "Wonderland" } }
{ "users": [ { "id": 1, "name": "John", "email": "john@example.com" }, { "id": 2, "name": "Jane", "email": "jane@example.com" } ] }
JavaScript 範例:
// JSON data as a string const jsonData = '{"name": "Alice", "age": 25}'; // Parse JSON into an object const user = JSON.parse(jsonData); console.log(user.name); // Output: Alice // Convert object to JSON const newJson = JSON.stringify(user); console.log(newJson); // Output: {"name":"Alice","age":25}
範例:PHP 陣列到 JSON:
<?php $data = [ "name" => "Alice", "age" => 25, "isStudent" => false, "skills" => ["PHP", "JavaScript", "HTML"], "address" => [ "street" => "123 Main St", "city" => "Wonderland" ] ]; // Convert PHP array to JSON $jsonData = json_encode($data, JSON_PRETTY_PRINT); echo $jsonData; ?>
範例:JSON 到 PHP 物件:
<?php $jsonData = '{ "name": "Alice", "age": 25, "isStudent": false, "skills": ["PHP", "JavaScript", "HTML"], "address": { "street": "123 Main St", "city": "Wonderland" } }'; // Convert JSON to PHP object $phpObject = json_decode($jsonData); echo $phpObject->name; // Output: Alice echo $phpObject->address->city; // Output: Wonderland ?>
範例:JSON 到 PHP 陣列:
<?php // Decode JSON to PHP array $phpArray = json_decode($jsonData, true); echo $phpArray['name']; // Output: Alice echo $phpArray['address']['city']; // Output: Wonderland ?>
虛擬艾斯
以上是大佬們的 JSON的詳細內容。更多資訊請關注PHP中文網其他相關文章!