JSON [Ký hiệu đối tượng JavaScript] là một định dạng trao đổi dữ liệu nhẹ. Nó dễ dàng được đọc và viết bởi con người và được máy móc phân tích cú pháp và tạo ra.
$ curl localhost:8000 John Doe is a gardener8 là loại phương tiện Internet chính thức cho JSON. Phần mở rộng tên tệp JSON là
$ curl localhost:8000 John Doe is a gardener9
Hàm
[ {"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"} ]0 trả về biểu diễn JSON của giá trị đã cho.
[ {"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"} ]1 lấy một chuỗi được mã hóa JSON và chuyển đổi nó thành một biến PHP
Các framework PHP như Symfony và Laravel có các phương thức tích hợp hoạt động với JSON
Mã hóa PHP JSON
Trong ví dụ sau, chúng tôi sử dụng hàm
[ {"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"} ]0
The example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.phpChúng tôi khởi động máy chủ và xác định vị trí của
[ {"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"} ]3Nhân vật. Chế độ xem JSON trong FirefoxCác trình duyệt web hiện đại hiển thị dạng xem JSON cho dữ liệu JSON khi chúng nhận được loại nội dung phù hợp trong tiêu đề của phản hồi
Trong ví dụ sau, chúng tôi sử dụng hàm
[ {"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"} ]1________số 8Chúng tôi bắt đầu máy chủ
$ curl localhost:8000 John Doe is a gardenerChúng tôi gửi một yêu cầu NHẬN với curl
PHP JSON được đọc từ tệp
Trong ví dụ sau, chúng tôi đọc dữ liệu JSON từ một tệp
[ {"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"} ]Đây là dữ liệu JSON
$ curl localhost:8000 John Doe is a gardener1Trong ví dụ mã, chúng tôi đọc tệp bằng
[ {"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"} ]5 và giải mã nó thành một mảng PHP bằng[ {"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"} ]1. Sau đó, chúng tôi đặt dữ liệu vào một bảng bằng cách sử dụng vòng lặp PHP foreachTrong ví dụ sau, chúng tôi đọc dữ liệu từ cơ sở dữ liệu SQLite và trả về dưới dạng JSON
$ curl localhost:8000 John Doe is a gardener4Mã SQL này tạo bảng
[ {"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"} ]7 trong SQLite$ curl localhost:8000 John Doe is a gardener6Với công cụ dòng lệnh
[ {"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"} ]8, chúng tôi tạo cơ sở dữ liệu SQLite và tạo bảng[ {"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"} ]7$ curl localhost:8000 John Doe is a gardener9Trong ví dụ, chúng tôi lấy dữ liệu từ cơ sở dữ liệu và trả về dưới dạng JSON
API tìm nạp PHP JSON và JS
Trong ví dụ sau, chúng tôi sử dụng API tìm nạp JavaScript để lấy dữ liệu JSON từ tập lệnh PHP
[ {"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"} ]Dữ liệu JSON được lưu trữ trong một tệp
The example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.php0Chúng tôi có một nút trong tài liệu. Khi chúng ta bấm vào nút, hàm
$ curl localhost:8000 John Doe is a gardener10 sẽ lấy dữ liệu JSON từ tập lệnh$ curl localhost:8000 John Doe is a gardener11. Một bảng HTML được tạo động và chứa đầy dữ liệuNhân vật. Bảng HTML chứa đầy dữ liệu JSON từ API tìm nạpTrong ví dụ sau, chúng tôi trả về dữ liệu JSON từ ứng dụng Slim
The example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.php1Chúng tôi cài đặt các gói
$ curl localhost:8000 John Doe is a gardener12,$ curl localhost:8000 John Doe is a gardener13 và$ curl localhost:8000 John Doe is a gardener14The example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.php2Chúng tôi sử dụng phương thức
$ curl localhost:8000 John Doe is a gardener15 của$ curl localhost:8000 John Doe is a gardener16 để trả về dữ liệu JSONPHP JSON trong Symfony
Trong ví dụ sau, chúng tôi gửi phản hồi JSON từ ứng dụng Symfony
The example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.php3Một ứng dụng mới được tạo
The example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.php4Chúng tôi cài đặt các thành phần
$ curl localhost:8000 John Doe is a gardener17,$ curl localhost:8000 John Doe is a gardener18 và$ curl localhost:8000 John Doe is a gardener19The example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.php5Chúng tôi thiết lập URL cơ sở dữ liệu cho cơ sở dữ liệu SQLite
The example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.php6Chúng tôi tạo cơ sở dữ liệu
src/Command/CreateCityTable. php
The example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.php7Đây là lệnh tạo bảng
[ {"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"} ]7 trong cơ sở dữ liệuThe example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.php8Lệnh được thực hiện
src/Kho lưu trữ/Kho lưu trữ thành phố. php
The example transforms a PHP array into a JSON string.
$ php -S localhost:8000 encode.php9Trong
$ curl localhost:8000 John Doe is a gardener41, chúng ta có phương thức$ curl localhost:8000 John Doe is a gardener42 truy xuất tất cả các hàng. Chúng tôi sử dụng Doctrine DBAL để thực hiện truy vấn cơ sở dữ liệusrc/Bộ điều khiển/Bộ điều khiển thành phố. php
The example transforms a JSON string into a PHP variable.
$ php -S localhost:8000 decode.php0Bên trong bộ điều khiển, chúng tôi lấy tất cả dữ liệu bằng cách sử dụng
$ curl localhost:8000 John Doe is a gardener41. Dữ liệu được chuyển đổi thành JSON với trình trợ giúp$ curl localhost:8000 John Doe is a gardener44The example transforms a JSON string into a PHP variable.
$ php -S localhost:8000 decode.php1Chúng tôi khởi động máy chủ web và định vị tới
$ curl localhost:8000 John Doe is a gardener45Trong ví dụ sau, chúng tôi gửi phản hồi JSON từ ứng dụng Laravel
The example transforms a JSON string into a PHP variable.
$ php -S localhost:8000 decode.php2Chúng tôi tạo một ứng dụng Laravel mới
The example transforms a JSON string into a PHP variable.
$ php -S localhost:8000 decode.php3Chúng tôi xác định kết nối và tệp cơ sở dữ liệu
The example transforms a JSON string into a PHP variable.
$ php -S localhost:8000 decode.php4Chúng tôi tạo tập tin cơ sở dữ liệu
The example transforms a JSON string into a PHP variable.
$ php -S localhost:8000 decode.php5Một lần di chuyển mới cho bảng
[ {"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"} ]7 đã được tạodatabase/migrations/2020_07_25_101535_create_cities_table. php
The example transforms a JSON string into a PHP variable.
$ php -S localhost:8000 decode.php6Trong tệp di chuyển, chúng tôi tạo một lược đồ cho bảng
[ {"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"} ]7The example transforms a JSON string into a PHP variable.
$ php -S localhost:8000 decode.php7Chúng tôi chạy di chuyển
The example transforms a JSON string into a PHP variable.
$ php -S localhost:8000 decode.php8Chúng tôi tạo một trình gieo dữ liệu cho cơ sở dữ liệu của chúng tôi
cơ sở dữ liệu/hạt giống/CitySeeder. php
The example transforms a JSON string into a PHP variable.
$ php -S localhost:8000 decode.php9Seeder chèn tám hàng vào bảng
$ curl localhost:8000 John Doe is a gardener0Chúng tôi thực hiện seeder
$ curl localhost:8000 John Doe is a gardener1Bên trong tuyến đường, chúng tôi chọn tất cả dữ liệu từ cơ sở dữ liệu và trả về dưới dạng JSON với trình trợ giúp
$ curl localhost:8000 John Doe is a gardener44$ curl localhost:8000 John Doe is a gardener2Chúng tôi chạy máy chủ web
$ curl localhost:8000 John Doe is a gardener3Chúng tôi lấy dữ liệu bằng công cụ
$ curl localhost:8000 John Doe is a gardener49Symfony cung cấp thành phần
$ curl localhost:8000 John Doe is a gardener60 cho phép chúng tôi tạo các yêu cầu HTTP trong PHP. Chúng tôi cũng có thể gửi dữ liệu JSON$ curl localhost:8000 John Doe is a gardener4Chúng tôi cài đặt thành phần
$ curl localhost:8000 John Doe is a gardener61$ curl localhost:8000 John Doe is a gardener5Ví dụ gửi yêu cầu GET với tải trọng JSON
src/Trình điều khiển/Chào điều khiển. php
$ curl localhost:8000 John Doe is a gardener6Trong
$ curl localhost:8000 John Doe is a gardener62, chúng tôi xử lý dữ liệu JSON và tạo một tin nhắn văn bản, tin nhắn này sẽ được trả về máy kháchLàm cách nào để tạo một đối tượng JSON trong PHP?
Tệp PHP được giải thích. .Chuyển đổi yêu cầu thành một đối tượng, sử dụng hàm PHP json_decode[]Truy cập cơ sở dữ liệu và điền vào một mảng với dữ liệu được yêu cầuThêm mảng vào một đối tượng và trả về đối tượng dưới dạng JSON bằng hàm json_encode[]Làm cách nào để ghi đối tượng JSON vào tệp trong PHP?
Viết tệp JSON bằng PHP .Chuyển đổi danh sách mảng kết hợp thành chuỗi JSON bằng json_encode[$jsonData, JSON_PRETTY_PRINT]Mở tệp JSON mới bằng fopen[$path, 'w']Viết chuỗi JSON bằng fwrite[$fp, $jsonString]Đóng tệp bằng fclose[$fp]Làm cách nào để mã hóa đối tượng JSON trong PHP?
PHP và JSON .Hàm json_encode[] dùng để mã hóa một giá trị sang định dạng JSONHàm json_decode[] được sử dụng để giải mã một đối tượng JSON thành một đối tượng PHP hoặc một mảng kết hợpHàm json_decode[] trả về một đối tượng theo mặc định. .Bạn cũng có thể lặp qua các giá trị bằng vòng lặp foreach[]Làm cách nào để tạo mảng JSON trong PHP?
Hàm json_encode[] dùng để chuyển đổi giá trị của mảng thành JSON . Chức năng này được thêm vào từ PHP5. Ngoài ra, bạn có thể tạo thêm các mảng lồng nhau theo yêu cầu của bạn. Bạn cũng có thể tạo một mảng các mảng đối tượng bằng chức năng này.