{"id":651,"date":"2025-06-11T12:03:23","date_gmt":"2025-06-11T04:03:23","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/xtywj\/651.html"},"modified":"2025-06-11T12:03:23","modified_gmt":"2025-06-11T04:03:23","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8php%e4%b8%ad%e8%af%bb%e5%8f%96%e5%92%8c%e5%86%99%e5%85%a5json%e6%96%87%e4%bb%b6","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/651.html","title":{"rendered":"\u5982\u4f55\u5728PHP\u4e2d\u8bfb\u53d6\u548c\u5199\u5165JSON\u6587\u4ef6"},"content":{"rendered":"<div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--1\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/dyA-1-400x300.jpg\" alt=\"Bug&#32534;&#35793;&#29422;\" class=\"wp-image-1842\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">Bug&#32534;&#35793;&#29422;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312;PHP&#20013;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#20869;&#32622;&#30340;<code>json_encode()<\/code>&#21644;<code>json_decode()<\/code>&#20989;&#25968;&#26469;&#22788;&#29702;JSON&#25968;&#25454;&#12290;&#20197;&#19979;&#26159;&#19968;&#20123;&#31034;&#20363;&#20195;&#30721;&#65292;&#23637;&#31034;&#20102;&#22914;&#20309;&#35835;&#21462;&#21644;&#20889;&#20837;JSON&#25991;&#20214;&#65306;<\/p>\n<h3>1. &#20889;&#20837;JSON&#25991;&#20214;<\/h3>\n<pre><code class=\"language-php\">&lt;?php\n\/\/ &#25968;&#25454;&#35201;&#23384;&#20648;&#20026;JSON&#26684;&#24335;\n$data = array(\n    \"name\" =&gt; \"John Doe\",\n    \"age\" =&gt; 30,\n    \"city\" =&gt; \"New York\"\n);\n\n\/\/ &#23558;&#25968;&#32452;&#36716;&#25442;&#20026;JSON&#23383;&#31526;&#20018;\n$jsonString = json_encode($data, JSON_PRETTY_PRINT); \/\/ &#20351;&#29992;JSON_PRETTY_PRINT&#36873;&#39033;&#20351;&#36755;&#20986;&#26356;&#26131;&#35835;\n\n\/\/ &#25351;&#23450;&#35201;&#20889;&#20837;&#30340;&#25991;&#20214;&#36335;&#24452;\n$filePath = 'data.json';\n\n\/\/ &#25171;&#24320;&#25991;&#20214;&#24182;&#20889;&#20837;JSON&#23383;&#31526;&#20018;\nfile_put_contents($filePath, $jsonString);\n\necho \"JSON&#25991;&#20214;&#24050;&#25104;&#21151;&#21019;&#24314;&#65281;\";\n?&gt;<\/code><\/pre>\n<h3>2. &#35835;&#21462;JSON&#25991;&#20214;<\/h3>\n<pre><code class=\"language-php\">&lt;?php\n\/\/ &#25351;&#23450;&#35201;&#35835;&#21462;&#30340;&#25991;&#20214;&#36335;&#24452;\n$filePath = 'data.json';\n\n\/\/ &#20174;&#25991;&#20214;&#20013;&#35835;&#21462;JSON&#23383;&#31526;&#20018;\n$jsonString = file_get_contents($filePath);\n\n\/\/ &#23558;JSON&#23383;&#31526;&#20018;&#35299;&#30721;&#20026;PHP&#25968;&#32452;\n$data = json_decode($jsonString, true);\n\n\/\/ &#36755;&#20986;&#35299;&#30721;&#21518;&#30340;&#25968;&#25454;\nprint_r($data);\n?&gt;<\/code><\/pre>\n<h3>&#31034;&#20363;&#20195;&#30721;&#35828;&#26126;<\/h3>\n<ul>\n<li>\n<p><strong>&#20889;&#20837;JSON&#25991;&#20214;<\/strong>&#65306;<\/p>\n<ul>\n<li><code>json_encode($data, JSON_PRETTY_PRINT)<\/code>&#65306;&#23558;PHP&#25968;&#32452;&#36716;&#25442;&#20026;JSON&#23383;&#31526;&#20018;&#65292;&#24182;&#20351;&#29992;<code>JSON_PRETTY_PRINT<\/code>&#36873;&#39033;&#20351;&#36755;&#20986;&#26356;&#26131;&#35835;&#12290;<\/li>\n<li><code>file_put_contents($filePath, $jsonString)<\/code>&#65306;&#23558;&#29983;&#25104;&#30340;JSON&#23383;&#31526;&#20018;&#20889;&#20837;&#25351;&#23450;&#30340;&#25991;&#20214;&#36335;&#24452;&#12290;<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>&#35835;&#21462;JSON&#25991;&#20214;<\/strong>&#65306;<\/p>\n<ul>\n<li><code>file_get_contents($filePath)<\/code>&#65306;&#20174;&#25351;&#23450;&#30340;&#25991;&#20214;&#36335;&#24452;&#35835;&#21462;JSON&#23383;&#31526;&#20018;&#12290;<\/li>\n<li><code>json_decode($jsonString, true)<\/code>&#65306;&#23558;JSON&#23383;&#31526;&#20018;&#35299;&#30721;&#20026;PHP&#25968;&#32452;&#12290;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&#36890;&#36807;&#36825;&#20123;&#31034;&#20363;&#20195;&#30721;&#65292;&#20320;&#21487;&#20197;&#36731;&#26494;&#22320;&#22312;PHP&#20013;&#35835;&#21462;&#21644;&#20889;&#20837;JSON&#25991;&#20214;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--2\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/zp-400x300.jpg\" alt=\"&#40657;&#26495;Bug&#35762;&#24072;\" class=\"wp-image-1849\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#40657;&#26495;Bug&#35762;&#24072;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<body><h2>&#20171;&#32461;<\/h2><p>&#22788;&#29702;JSON&#25968;&#25454;&#26159;&#29616;&#20195;Web&#24320;&#21457;&#20013;&#30340;&#19968;&#20010;&#37325;&#35201;&#37096;&#20998;&#12290;&#22312;PHP&#20013;&#65292;&#35835;&#21462;&#21644;&#20889;&#20837;JSON&#25991;&#20214;&#21487;&#20197;&#36890;&#36807;&#20869;&#32622;&#20989;&#25968;&#36731;&#26494;&#31649;&#29702;&#12290;&#26412;&#25945;&#31243;&#23558;&#19968;&#27493;&#27493;&#24102;&#20320;&#20102;&#35299;&#36825;&#20123;&#36807;&#31243;&#12290;<\/p><h2>&#38405;&#35835;JSON&#25991;&#20214;<\/h2><p>&#35753;&#25105;&#20204;&#20174;PHP&#20013;&#35835;&#21462;JSON&#25991;&#20214;&#24320;&#22987;&#12290;&#20197;&#19979;&#31034;&#20363;&#28436;&#31034;&#20102;&#22914;&#20309;&#20174;&#25991;&#20214;&#20013;&#21152;&#36733;JSON&#25968;&#25454;&#65306;<\/p><pre><code>&lt;?php\n\/\/ The JSON file\n$filename = 'data.json';\n\/\/ Read the file into a variable\n$jsonData = file_get_contents($filename);\n\/\/ Decode the JSON data into a PHP associative array\n$dataArray = json_decode($jsonData, true);\nprint_r($dataArray);\n?&gt;<\/code><\/pre><p>&#36825;&#27573;&#20195;&#30721;&#20250;&#36755;&#20986;&#8221;data.json&#8221;&#25991;&#20214;&#30340;&#20869;&#23481;&#65292;&#20197;&#20851;&#32852;&#25968;&#32452;&#30340;&#24418;&#24335;&#21576;&#29616;&#65292;&#35753;&#20320;&#20687;&#35775;&#38382;&#20854;&#20182;PHP&#25968;&#32452;&#19968;&#26679;&#26469;&#35775;&#38382;JSON&#25968;&#25454;&#12290;<\/p><h2>&#25776;&#20889;&#21040;JSON&#25991;&#20214;&#20013;<\/h2><p>&#29616;&#22312;&#65292;&#35753;&#25105;&#20204;&#30475;&#30475;&#22914;&#20309;&#22312;PHP&#20013;&#20889;&#20837;JSON&#25991;&#20214;&#12290;&#36825;&#19982;&#20174;&#19968;&#20010;&#25991;&#20214;&#35835;&#21462;&#19968;&#26679;&#31616;&#21333;&#65306;<\/p><pre><code>&lt;?php\n\/\/ An associative array of data\n$dataArray = [\n    'key1' =&gt; 'value1',\n    'key2' =&gt; 'value2',\n];\n\n\/\/ Encode the data as JSON\n$jsonData = json_encode($dataArray, JSON_PRETTY_PRINT);\n\n\/\/ The JSON file where to write\n$filename = 'output.json';\n\n\/\/ Write the JSON data to a file\nfile_put_contents($filename, $jsonData);\n?&gt;<\/code><\/pre><p>&#36825;&#27573;&#20195;&#30721;&#29255;&#27573;&#20250;&#20197;&#26684;&#24335;&#21270;&#30340;&#26041;&#24335;&#23558;&#20851;&#32852;&#25968;&#32452;&#20889;&#20837;&#21517;&#20026;&ldquo;output.json&rdquo;&#30340;&#25991;&#20214;&#20013;&#65292;&#36825;&#26159;&#22240;&#20026;&#20351;&#29992;&#20102;&ldquo;JSON_PRETTY_PRINT&rdquo;&#26631;&#24535;&#12290;<\/p><h2>&#22788;&#29702;&#22797;&#26434;&#30340;JSON&#32467;&#26500;<\/h2><p>&#22788;&#29702;&#26356;&#22797;&#26434;&#30340;JSON&#32467;&#26500;&#21487;&#33021;&#38656;&#35201;&#39069;&#22806;&#30340;&#32771;&#34385;&#12290;&#35753;&#25105;&#20204;&#26469;&#30475;&#19968;&#20010;&#28436;&#31034;&#22914;&#20309;&#22788;&#29702;&#23884;&#22871;JSON&#30340;&#20195;&#30721;&#29255;&#27573;&#65306;<\/p><pre><code>&lt;?php\n\/\/ A complex associative array (for example, nested arrays)\n$complexDataArray = [\n    'person' =&gt; [\n        'name' =&gt; 'John Doe',\n        'age' =&gt; 30,\n        'job' =&gt; 'Developer'\n    ],\n    'skills' =&gt; ['PHP', 'JavaScript', 'MySQL'],\n    'active' =&gt; true\n];\n\n\/\/ encode the array to a JSON string\n$jsonData = json_encode($complexDataArray, JSON_PRETTY_PRINT);\n\n\/\/ The JSON file\n$filename = 'complex_data.json';\n\n\/\/ Write the JSON string to the file\nfile_put_contents($filename, $jsonData);\n\n\/\/ Now read and convert it back to an associative array\n$readJson = file_get_contents($filename);\n$readArray = json_decode($readJson, true);\n\necho '&lt;pre&gt;';\nprint_r($readArray);\necho '&lt;\/pre&gt;';\n?&gt;<\/code><\/pre><p>&#36825;&#20010;&#20195;&#30721;&#20250;&#21019;&#24314;&#19968;&#20010;&#26356;&#22797;&#26434;&#30340;JSON&#25991;&#20214;&#65292;&#28982;&#21518;&#35835;&#21462;&#23427;&#24182;&#25552;&#20379;&#28165;&#26224;&#30340;PHP&#25968;&#32452;&#32467;&#26500;&#12290;<\/p><h2>&#38169;&#35823;&#22788;&#29702;&#22312;JSON&#35835;&#21462;\/&#20889;&#20837;&#20013;&#30340;&#24212;&#29992;<\/h2><p>&#27809;&#26377;&#25945;&#31243;&#26159;&#23436;&#25972;&#30340;&#65292;&#19981;&#35752;&#35770;&#38169;&#35823;&#22788;&#29702;&#12290;&#22312;PHP&#20013;&#22788;&#29702;JSON&#25991;&#20214;&#26102;&#65292;&#22914;&#20309;&#23454;&#29616;&#22522;&#26412;&#30340;&#38169;&#35823;&#26816;&#26597;&#22914;&#19979;&#65306;<\/p><pre><code>&lt;?php\n\/\/ Loading JSON data\n$jsonData = @file_get_contents($filename);\n\nif ($jsonData === false) {\n    die('Error reading the JSON file');\n}\n\n\/\/ Decoding JSON\n$dataArray = json_decode($jsonData, true);\n\nif (json_last_error() !== JSON_ERROR_NONE) {\n    die('Error decoding JSON');\n}\n\n\/\/ Encoding JSON\n$jsonData = json_encode($dataArray, JSON_PRETTY_PRINT);\n\nif ($jsonData === false) {\n    die('Error encoding JSON');\n}\n\n\/\/ Writing JSON\nif (@file_put_contents($filename, $jsonData) === false) {\n    die('Error writing the JSON file');\n}\n?&gt;<\/code><\/pre><p>&#35813;&#29255;&#27573;&#23558;&#22312;&#35835;&#21462;&#25110;&#20889;&#20837;&#36807;&#31243;&#20013;&#20986;&#29616;&#20219;&#20309;&#38169;&#35823;&#26102;&#65292;&#20197;&#36866;&#24403;&#30340;&#28040;&#24687;&#24418;&#24335;&#32456;&#27490;&#25191;&#34892;&#65292;&#36890;&#36807;&#26816;&#26597;&#36820;&#22238;&#20540;&#24182;&#21033;&#29992;json_last_error()&#20989;&#25968;&#36827;&#34892;JSON&#35299;&#30721;&#12290;<\/p><h2>&#19982;JSON&#25991;&#20214;&#38145;&#23450;&#24037;&#20316;<\/h2><p>&#22312;&#22810;&#29992;&#25143;&#29615;&#22659;&#20013;&#22788;&#29702;&#25991;&#20214;&#25805;&#20316;&#26102;&#65292;&#29702;&#35299;&#24182;&#23454;&#26045;&#25991;&#20214;&#38145;&#23450;&#33267;&#20851;&#37325;&#35201;&#12290;&#36825;&#30830;&#20445;&#20102;&#22810;&#20010;&#36827;&#31243;&#19981;&#20250;&#21516;&#26102;&#20889;&#20837;&#25110;&#35835;&#21462;&#21516;&#19968;&#25991;&#20214;&#65292;&#20174;&#32780;&#36991;&#20813;&#25968;&#25454;&#25439;&#22351;&#12290;&#20351;&#29992;PHP&#35835;&#20889;JSON&#25991;&#20214;&#26102;&#65292;&#35831;&#23398;&#20064;&#22914;&#20309;&#36827;&#34892;&#25991;&#20214;&#38145;&#23450;&#21644;&#35299;&#38145;&#12290;<\/p><pre><code>&lt;?php\r\n\r\nclass JsonFileManager\r\n{\r\n    private $filePath;\r\n\r\n    public function __construct($filePath)\r\n    {\r\n        $this-&gt;filePath = $filePath;\r\n    }\r\n\r\n    public function readJsonFile(): array\r\n    {\r\n        $this-&gt;acquireLock();\r\n        \r\n        \/\/ Read JSON file\r\n        $jsonData = file_get_contents($this-&gt;filePath);\r\n        $decodedData = json_decode($jsonData, true);\r\n\r\n        $this-&gt;releaseLock();\r\n\r\n        return $decodedData ?? [];\r\n    }\r\n\r\n    public function writeJsonFile(array $data): void\r\n    {\r\n        $this-&gt;acquireLock();\r\n\r\n        \/\/ Convert data to JSON\r\n        $jsonData = json_encode($data, JSON_PRETTY_PRINT);\r\n\r\n        \/\/ Write to JSON file\r\n        file_put_contents($this-&gt;filePath, $jsonData);\r\n\r\n        $this-&gt;releaseLock();\r\n    }\r\n\r\n    private function acquireLock(): void\r\n    {\r\n        $lockFile = $this-&gt;filePath . '.lock';\r\n\r\n        \/\/ Attempt to acquire an exclusive lock\r\n        $lockHandle = fopen($lockFile, 'w');\r\n        flock($lockHandle, LOCK_EX);\r\n    }\r\n\r\n    private function releaseLock(): void\r\n    {\r\n        $lockFile = $this-&gt;filePath . '.lock';\r\n\r\n        \/\/ Release the lock\r\n        fclose($lockFile);\r\n        unlink($lockFile);\r\n    }\r\n}\r\n\r\n\/\/ Example usage\r\n$jsonFilePath = 'example.json';\r\n$jsonFileManager = new JsonFileManager($jsonFilePath);\r\n\r\n\/\/ Read JSON file\r\n$data = $jsonFileManager-&gt;readJsonFile();\r\nvar_dump($data);\r\n\r\n\/\/ Modify data\r\n$data['newKey'] = 'New Value';\r\n\r\n\/\/ Write back to JSON file\r\n$jsonFileManager-&gt;writeJsonFile($data);\r<\/code><\/pre><p>&#35813;&#20195;&#30721;&#23450;&#20041;&#20102;&#19968;&#20010;&hellip;&hellip;<code>JsonFileManager<\/code>&#35813;&#31867;&#25552;&#20379;&#20102;&#35835;&#21462;&#21644;&#20889;&#20837;JSON&#25991;&#20214;&#30340;&#26041;&#27861;&#65292;&#20197;&#21450;&#25991;&#20214;&#38145;&#23450;&#21151;&#33021;&#12290;<code>acquireLock<\/code>&#26041;&#27861;&#36127;&#36131;&#33719;&#21462;&#29420;&#21344;&#38145;&#65292;&#32780;<code>releaseLock<\/code>&#26041;&#27861;&#37322;&#25918;&#38145;&#65292;&#30830;&#20445;&#22312;&#22810;&#29992;&#25143;&#29615;&#22659;&#20013;&#23545;JSON&#25991;&#20214;&#30340;&#23433;&#20840;&#24182;&#21457;&#35775;&#38382;&#12290;<\/p><p>&#36825;&#26159;&#26368;&#21518;&#19968;&#27493;&#20102;&#65292;&#24841;&#24555;&#32534;&#31243;&#65292;&#20170;&#22825;&#36807;&#24471;&#24841;&#24555;&#65281;<\/p><\/body>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--3\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/xygcfznnzczhsdmwydzhsmzzzmddnq-400x300.jpg\" alt=\"&#38271;&#24037;&#30721;&#22900;1523\" class=\"wp-image-2906\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#38271;&#24037;&#30721;&#22900;1523<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312; PHP &#20013;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#20869;&#32622;&#30340; <code>json<\/code> &#27169;&#22359;&#26469;&#22788;&#29702; JSON &#25968;&#25454;&#12290;&#20197;&#19979;&#26159;&#22914;&#20309;&#35835;&#21462;&#21644;&#20889;&#20837; JSON &#25991;&#20214;&#30340;&#20004;&#20010;&#31034;&#20363;&#65306;<\/p>\n<h3>1. &#20174;&#25991;&#20214;&#20013;&#35835;&#21462; JSON<\/h3>\n<p>&#39318;&#20808;&#65292;&#30830;&#20445;&#20320;&#24050;&#32463;&#23433;&#35013;&#20102; <code>json<\/code> &#27169;&#22359;&#12290;&#22914;&#26524;&#27809;&#26377;&#65292;&#35831;&#36890;&#36807; Composer &#23433;&#35013;&#23427;&#65306;<\/p>\n<pre><code class=\"language-bash\">composer require php-optional\/php-optional<\/code><\/pre>\n<p>&#28982;&#21518;&#65292;&#21487;&#20197;&#20351;&#29992;&#20197;&#19979;&#26041;&#27861;&#20174;&#25991;&#20214;&#20013;&#35835;&#21462; JSON &#25968;&#25454;&#65306;<\/p>\n<pre><code class=\"language-php\">&lt;?php\nrequire 'vendor\/autoload.php';\n\nuse JsonSerializable;\n\nclass MyClass implements JsonSerializable {\n    \/\/ &#23454;&#29616; JsonSerializable &#25509;&#21475;&#30340;&#26041;&#27861;\n}\n\n$filename = 'data.json';\n$json_data = file_get_contents($filename);\n$data = json_decode($json_data, true);\n\n\/\/ &#20351;&#29992; $data &#23545;&#35937;&#25191;&#34892;&#20854;&#20182;&#25805;&#20316;&#65288;&#20363;&#22914;&#65306;&#39564;&#35777;&#25968;&#25454;&#26684;&#24335;&#65289;<\/code><\/pre>\n<h3>2. &#23558; PHP &#32467;&#26500;&#21270;&#25968;&#25454;&#20889;&#20837; JSON &#25991;&#20214;<\/h3>\n<p>&#35201;&#23558; PHP &#32467;&#26500;&#21270;&#30340;&#25968;&#25454;&#65288;&#22914;&#25968;&#32452;&#65289;&#20889;&#20837; JSON &#25991;&#20214;&#65292;&#21487;&#20197;&#20351;&#29992;&#20197;&#19979;&#26041;&#27861;&#65306;<\/p>\n<pre><code class=\"language-php\">&lt;?php\nrequire 'vendor\/autoload.php';\n\nuse JsonSerializable;\n\nclass MyClass implements JsonSerializable {\n    public $property1;\n    public $property2;\n    public $property3;\n    \/\/ &#30465;&#30053;&#20854;&#20182;&#23646;&#24615;\n\n    public function jsonSerialize() {\n        return [\n            'property1' =&gt; $this-&gt;property1,\n            'property2' =&gt; $this-&gt;property2,\n            'property3' =&gt; $this-&gt;property3,\n            \/\/ ... &#20854;&#20182;&#23646;&#24615;\n        ];\n    }\n}\n\n$myObject = new MyClass();\n$myObject-&gt;property1 = 'Hello World!';\n$myObject-&gt;property2 = 'This is a test.';\n$myObject-&gt;property3 = 'It works!';\n\n$json_string = json_encode($myObject, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);\nfile_put_contents('output.json', $json_string);<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#25105;&#20204;&#21019;&#24314;&#20102;&#19968;&#20010;&#21517;&#20026; <code>MyClass<\/code> &#30340;&#31867;&#65292;&#24182;&#23454;&#29616;&#20102; <code>JsonSerializable<\/code> &#25509;&#21475;&#12290;&#28982;&#21518;&#65292;&#25105;&#20204;&#23454;&#20363;&#21270;&#36825;&#20010;&#31867;&#24182;&#35774;&#32622;&#20102;&#20960;&#20010;&#23646;&#24615;&#12290;<\/p>\n<p>&#26368;&#21518;&#65292;&#25105;&#20204;&#23558;&#23545;&#35937;&#36716;&#25442;&#20026; JSON &#23383;&#31526;&#20018;&#65292;&#24182;&#23558;&#20854;&#20889;&#20837;&#21040;&#21517;&#20026; <code>output.json<\/code> &#30340;&#25991;&#20214;&#20013;&#12290;&#27880;&#24847;&#65292;<code>JSON_PRETTY_PRINT<\/code> &#21442;&#25968;&#29992;&#20110;&#20351;&#36755;&#20986;&#30340; JSON &#25991;&#26412;&#26356;&#21152;&#26131;&#35835;&#65307;<code>JSON_UNESCAPED_SLASHES<\/code> &#21442;&#25968;&#20801;&#35768;&#22312; JSON &#23383;&#31526;&#20018;&#20013;&#19981;&#36716;&#20041;&#21333;&#24341;&#21495;&#12290;<\/p>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<pre><code class=\"language-php\">&lt;?php\nrequire 'vendor\/autoload.php';\n\nuse JsonSerializable;\n\nclass MyClass implements JsonSerializable {\n    public $property1;\n    public $property2;\n    public $property3;\n    \/\/ &#30465;&#30053;&#20854;&#20182;&#23646;&#24615;\n\n    public function jsonSerialize() {\n        return [\n            'property1' =&gt; $this-&gt;property1,\n            'property2' =&gt; $this-&gt;property2,\n            'property3' =&gt; $this-&gt;property3,\n            \/\/ ... &#20854;&#20182;&#23646;&#24615;\n        ];\n    }\n}\n\n$myObject = new MyClass();\n$myObject-&gt;property1 = 'Hello World!';\n$myObject-&gt;property2 = 'This is a test.';\n$myObject-&gt;property3 = 'It works!';\n\n$json_string = json_encode($myObject, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);\nfile_put_contents('output.json', $json_string);\n?&gt;<\/code><\/pre>\n<h3>&#27880;&#24847;&#20107;&#39033;<\/h3>\n<ul>\n<li>&#22312; PHP &#20013;&#22788;&#29702; JSON &#26102;&#65292;&#30830;&#20445;&#20320;&#30340;&#33050;&#26412;&#20855;&#26377;&#27491;&#30830;&#30340;&#26435;&#38480;&#20197;&#35775;&#38382;&#25991;&#20214;&#12290;<\/li>\n<li>&#22914;&#26524;&#25991;&#20214;&#24050;&#23384;&#22312;&#65292;&#21487;&#33021;&#20250;&#35206;&#30422;&#21407;&#26377;&#30340;&#25968;&#25454;&#12290;&#20026;&#20102;&#36991;&#20813;&#36825;&#31181;&#24773;&#20917;&#65292;&#21487;&#20197;&#22312;&#20889;&#20837;&#21069;&#26816;&#26597;&#25991;&#20214;&#26159;&#21542;&#23384;&#22312;&#12290;<\/li>\n<li><code>json_decode()<\/code> &#21644; <code>json_encode()<\/code> &#20989;&#25968;&#20250;&#33258;&#21160;&#22788;&#29702;&#20219;&#20309;&#28508;&#22312;&#30340; JSON &#38169;&#35823;&#12290;&#22914;&#26524;&#36935;&#21040;&#38169;&#35823;&#65292;&#23427;&#20204;&#20250;&#25243;&#20986;&#24322;&#24120;&#65292;&#32780;&#19981;&#26159;&#36820;&#22238;&#38169;&#35823;&#20449;&#24687;&#12290;<\/li>\n<\/ul>\n<p>&#24076;&#26395;&#36825;&#20123;&#31034;&#20363;&#26377;&#21161;&#20110;&#20320;&#22312; PHP &#20013;&#26356;&#26377;&#25928;&#22320;&#24037;&#20316;&#19982; JSON &#25968;&#25454;&#65281;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#22312;PHP&#20013;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#20869;&#32622;&#30340;json_encode()&#21644;json_decode()&#038;#20989..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[42],"tuisongtax":[],"class_list":["post-651","my1js","type-my1js","status-publish","hentry","my1js2nav-xtywj"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=651"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=651"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=651"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}