PHP tạo đối tượng JSON

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 gardener
8 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 gardener
9

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.php

Chú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"}
]
3

Nhân vật. Chế độ xem JSON trong Firefox

Cá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ố 8

Chúng tôi bắt đầu máy chủ

$ curl localhost:8000
John Doe is a gardener

Chú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 gardener
1

Trong 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 foreach

Trong 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 gardener
4

Mã 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 gardener
6

Vớ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 gardener
9

Trong 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.php
0

Chú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 gardener
10 sẽ lấy dữ liệu JSON từ tập lệnh
$ curl localhost:8000
John Doe is a gardener
11. Một bảng HTML được tạo động và chứa đầy dữ liệu

Nhân vật. Bảng HTML chứa đầy dữ liệu JSON từ API tìm nạp

Trong 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.php
1

Chúng tôi cài đặt các gói

$ curl localhost:8000
John Doe is a gardener
12,
$ curl localhost:8000
John Doe is a gardener
13 và
$ curl localhost:8000
John Doe is a gardener
14

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
2

Chúng tôi sử dụng phương thức

$ curl localhost:8000
John Doe is a gardener
15 của
$ curl localhost:8000
John Doe is a gardener
16 để trả về dữ liệu JSON

PHP 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.php
3

Một ứng dụng mới được tạo

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
4

Chúng tôi cài đặt các thành phần

$ curl localhost:8000
John Doe is a gardener
17,
$ curl localhost:8000
John Doe is a gardener
18 và
$ curl localhost:8000
John Doe is a gardener
19

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
5

Chú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.php
6

Chú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.php
7

Đâ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ệu

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
8

Lệ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.php
9

Trong

$ curl localhost:8000
John Doe is a gardener
41, chúng ta có phương thức
$ curl localhost:8000
John Doe is a gardener
42 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ệu

src/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.php
0

Bê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 gardener
41. Dữ liệu được chuyển đổi thành JSON với trình trợ giúp
$ curl localhost:8000
John Doe is a gardener
44

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
1

Chúng tôi khởi động máy chủ web và định vị tới

$ curl localhost:8000
John Doe is a gardener
45

Trong 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.php
2

Chú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.php
3

Chú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.php
4

Chú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.php
5

Mộ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ạo

database/migrations/2020_07_25_101535_create_cities_table. php

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
6

Trong 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"}
]
7

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
7

Chúng tôi chạy di chuyển

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
8

Chú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.php
9

Seeder chèn tám hàng vào bảng

$ curl localhost:8000
John Doe is a gardener
0

Chúng tôi thực hiện seeder

$ curl localhost:8000
John Doe is a gardener
1

Bê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 gardener
44

$ curl localhost:8000
John Doe is a gardener
2

Chúng tôi chạy máy chủ web

$ curl localhost:8000
John Doe is a gardener
3

Chúng tôi lấy dữ liệu bằng công cụ

$ curl localhost:8000
John Doe is a gardener
49

Symfony cung cấp thành phần

$ curl localhost:8000
John Doe is a gardener
60 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 gardener
4

Chúng tôi cài đặt thành phần

$ curl localhost:8000
John Doe is a gardener
61

$ curl localhost:8000
John Doe is a gardener
5

Ví 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 gardener
6

Trong

$ curl localhost:8000
John Doe is a gardener
62, 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ách

Là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ầu
Thê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 JSON
Hà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ợp
Hà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.

Chủ Đề