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中文网其他相关文章!