JSONJSON (JavaScript Object Notation) is a lightweight data-interchange format. Itis easily read and written by humans and parsed and generated by machines. Theapplication/json is the official Internet media type for JSON. TheJSON filename extension is .json. Bạn đang xem: Xử lý json trong php dùng Để làm gì? xử lý json trong php The json_encode function returns the JSON representation of thegiven value. The json_decode takes a JSON encoded string andconverts it into a PHP variable. PHP frameworks such as Symfony and Laravel have built-in methods that workwith JSON. PHP JSON encodeIn the following example, we use the json_encode function. The example transforms a PHP array into a JSON string. $ php -S localhost:8000 encode.phpWe start the server and locate to the localhost:8000. Figure: JSON view in Firefox Modern web browsers show JSON view for JSON data when they receive an appropriatecontent type in the header of the response. PHP JSON decodeIn the following example, we use the json_decode function. The example transforms a JSON string into a PHP variable. Xem thêm: Tải Game Vxp Free - Cửa Hàng Game & Apps $ php -S localhost:8000 decode.phpWe start the server. $ curl localhost:8000John Doe is a gardenerWe send a GET request with curl. < {"name": "John Doe", "occupation": "gardener", "country": "USA"}, {"name": "Richard Roe", "occupation": "driver", "country": "UK"}, {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"}, {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"}, {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"}, {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}>This is the JSON data. Name Occupation Country
In the code example, we read the file with file_get_contents anddecode it into an PHP array with json_decode. Later, we placethe data into a table utilizing PHP foreach loop. PHP JSON read from databaseIn the following example, we read data from an SQLite database and returnit in JSON. BEGIN TRANSACTION;DROP TABLE IF EXISTS cities;CREATE TABLE cities(id INTEGER PRIMARY KEY, name TEXT, population INTEGER);INSERT INTO cities(name, population) VALUES("Bratislava", 432000);INSERT INTO cities(name, population) VALUES("Budapest", 1759000);INSERT INTO cities(name, population) VALUES("Prague", 1280000);INSERT INTO cities(name, population) VALUES("Warsaw", 1748000);INSERT INTO cities(name, population) VALUES("Los Angeles", 3971000);INSERT INTO cities(name, population) VALUES("New York", 8550000);INSERT INTO cities(name, population) VALUES("Edinburgh", 464000);INSERT INTO cities(name, population) VALUES("Berlin", 3671000);COMMIT;This SQL code creates a cities table in SQLite. $ sqlite3 test.dbsqlite> .read cities.sqlsqlite> SELECT * FROM cities;1|Bratislava|4320002|Budapest|17590003|Prague|12800004|Warsaw|17480005|Los Angeles|39710006|New York|85500007|Edinburgh|4640008|Berlin|3671000With the sqlite3 command line tool, we generate an SQLite databaseand create the cities table. query("SELECT * FROM cities");$cities = <>;while ($row = $res->fetchArray()) { $cities<> = $row;}header("Content-type:application/json;charset=utf-8");echo json_encode(<"cities" => $cities>);In the example, we retrieve the data from the database and return it as JSON. PHP JSON and JS fetch APIIn the following example, we use JavaScript fetch API to get the JSON data from a PHP script.
|