Hướng dẫn object to json php
Chuyển array thành json stringArray thành json string
Kết quả: Show
Nếu muốn format đẹp hơn ta cho thêm tham số:
Kết quả:
Chuyển ngược lại json string thành arrayCách thực hiện đơn giản như sau:
Tham số thứ 2 true của hàm json_decode là để hàm trả về kết quả là array. Chuyển Object thành json stringEncodeTa giả sử có class user như sau:
Sử dụng class như sau:
Ta cũng sẽ được kết quả tương tự như trên: `{"id":1,"username":"admin"} DecodeVậy làm sao để decode được json to object của class User Có khá nhiều cách nhưng cách đơn giản và hiệu quả nhất như sau:
Kết quả:
--- Phuc Tran Hoang (PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL json >= 1.2.0) json_encode — Returns the JSON representation of a value Descriptionjson_encode(mixed If a value to be serialized is an object, then by default only publicly visible properties will be included. Alternatively, a class may implement JsonSerializable to control how its values are serialized to JSON. The encoding is affected by the supplied Parametersvalue The All string data must be UTF-8 encoded.
flags Bitmask consisting of depth Set the maximum depth. Must be greater than zero. Return Values Returns a JSON encoded string on success or Changelog
ExamplesExample #1 A json_encode() example
json_encode($arr); The above example will output: {"a":1,"b":2,"c":3,"d":4,"e":5} Example #2 A json_encode() example showing some flags in use
"Normal: ", json_encode($a), "\n"; The above example will output: Normal: [" Example #3 JSON_NUMERIC_CHECK option example
The above example will output something similar to: Strings representing numbers automatically turned into numbers array(4) { [0]=> string(7) "+123123" [1]=> string(7) "-123123" [2]=> string(5) "1.2e3" [3]=> string(7) "0.00001" } string(28) "[123123,-123123,1200,1.0e-5]" Strings containing improperly formatted numbers array(2) { [0]=> string(13) "+a33123456789" [1]=> string(4) "a123" } string(24) "["+a33123456789","a123"]" Example #4 Sequential versus non-sequential array example
PHP_EOL."Non-sequential array".PHP_EOL; The above example will output: Sequential array array(4) { [0]=> string(3) "foo" [1]=> string(3) "bar" [2]=> string(3) "baz" [3]=> string(5) "blong" } string(27) "["foo","bar","baz","blong"]" Non-sequential array array(4) { [1]=> string(3) "foo" [2]=> string(3) "bar" [3]=> string(3) "baz" [4]=> string(5) "blong" } string(43) "{"1":"foo","2":"bar","3":"baz","4":"blong"}" Sequential array with one key unset array(3) { [0]=> string(3) "foo" [2]=> string(3) "baz" [3]=> string(5) "blong" } string(33) "{"0":"foo","2":"baz","3":"blong"}" Example #5
The above example will output: string(4) "12.0" string(2) "12" Notes
See Also
bohwaz ¶ 10 years ago
DimeCadmium ¶ 8 months ago
ryan at ryanparman dot com ¶ 12 years ago
Nick ¶ 6 years ago
guilhenfsu at gmail dot com ¶ 9 years ago
ck at ergovia dot de ¶ 9 years ago
Walter Tross ¶ 6 years ago
Istratov Vadim ¶ 13 years ago
jakepucan at gmail dot com ¶ 1 year ago
Sam Barnum ¶ 13 years ago
spam.goes.in.here AT gmail.com ¶ 14 years ago
Garrett ¶ 13 years ago
pvl dot kolensikov at gmail dot com ¶ 11 years ago
__construct(){
mikko dot rantalainen at peda dot net ¶ 1 year ago
|