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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号