Hướng dẫn php json_encode array of objects - mảng đối tượng php json_encode

Theo mặc định, json_encode () chỉ tuần tự hóa các thuộc tính công khai của một đối tượng. Làm cho tất cả các thuộc tính bạn muốn công khai tuần tự không phải là giải pháp! Php 5.4 và sau đó có giao diện Jsonserializable, nhưng tôi đề xuất một giải pháp đơn giản cho các phiên bản PHP trước đó.

Vì Jsonserializable chỉ là một phần của Php 5.4 trở lên, nên tự tạo nó.

if (!interface_exists('JsonSerializable')) {
   interface JsonSerializable {
      public function jsonSerialize();
   }
}

Điều đó không khó lắm, phải không? Bây giờ chúng ta có thể triển khai Jsonserializable mà không phải lo lắng về phiên bản PHP mà chúng ta đang sử dụng!

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}

Bây giờ bạn chỉ có thể gọi jsonserialize () để có được một mảng kết hợp mà bạn có thể mã hóa với json_encode().

    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...

Sau đó, bạn có thể gọi

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
0 để có được kết quả mong muốn của bạn.

[
   {
      "day":"sunday",
      "date":"2012-03-06",
      "target":"\u0634\u0633\u064a",
      "id":1,
      "status":0,
      "time":"12:00:00"
   },
   {
      "day":"sunday",
      "date":"2012-03-06",
      "target":"clinnics",
      "id":4,
      "status":0,
      "time":"00:00:00"
   },
   ...
]

Mikko Dot Rantalainen tại Peda Dot Net

Hàm PHP json_encode () làm gì?Returns the JSON representation of a value

Hàm json_encode () được sử dụng để mã hóa một giá trị cho định dạng JSON.

Bạn có thể JSON mã hóa một đối tượng trong PHP không?(mixed

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
1, int
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
2 = 0
, int
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
3 = 512
): string|false

Để mã hóa các đối tượng thành chuỗi được định dạng JSON trong PHP, bạn có thể sử dụng hàm json_encode (giá trị, tùy chọn, độ sâu). Tham số đầu tiên chỉ định đối tượng PHP để mã hóa. Bạn có thể kiểm soát cách đối tượng PHP sẽ được mã hóa thành JSON bằng cách chuyển kết hợp bitmasks trong tham số thứ hai.JsonSerializable to control how its values are serialized to JSON.

Sự khác biệt giữa json_encode và json_decode là gì?

Tôi nghĩ rằng json_encode đảm bảo rằng PHP có thể đọc tệp .json nhưng bạn phải chỉ định một tên biến, trong khi với JSON_DECODE, nó giống nhau nhưng bạn phải chỉ định tên tệp.

JSON_ENCODE trở lại là gì?

Hàm json_encode () có thể trả về một chuỗi chứa biểu diễn JSON của giá trị được cung cấp. Mã hóa bị ảnh hưởng bởi các tùy chọn được cung cấp, và ngoài ra, việc mã hóa các giá trị phao phụ thuộc vào giá trị của serialize_precision.

(Php 5> = 5.2.0, Php 7, Php 8, Pecl Json> = 1.2.0)

JSON_ENCODE - Trả về biểu diễn JSON của một giá trị:

Sự mô tả

JSON_ENCODE (hỗn hợp
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
1, int
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
2 = 0, int
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
3 = 512): Chuỗi | Sai

Nếu một giá trị được tuần tự hóa là một đối tượng, thì theo mặc định, chỉ bao gồm các thuộc tính hiển thị công khai sẽ được bao gồm. Ngoài ra, một lớp có thể triển khai jsonsonserializable để kiểm soát cách các giá trị của nó được tuần tự hóa thành JSON.

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
8,
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
9
,
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
0
,
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
1
,
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
2
,
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
3
,
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
4
,
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
5
,
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
6
,
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
7
,
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
8
,
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
9
,
[
   {
      "day":"sunday",
      "date":"2012-03-06",
      "target":"\u0634\u0633\u064a",
      "id":1,
      "status":0,
      "time":"12:00:00"
   },
   {
      "day":"sunday",
      "date":"2012-03-06",
      "target":"clinnics",
      "id":4,
      "status":0,
      "time":"00:00:00"
   },
   ...
]
0
,
[
   {
      "day":"sunday",
      "date":"2012-03-06",
      "target":"\u0634\u0633\u064a",
      "id":1,
      "status":0,
      "time":"12:00:00"
   },
   {
      "day":"sunday",
      "date":"2012-03-06",
      "target":"clinnics",
      "id":4,
      "status":0,
      "time":"00:00:00"
   },
   ...
]
1
,
[
   {
      "day":"sunday",
      "date":"2012-03-06",
      "target":"\u0634\u0633\u064a",
      "id":1,
      "status":0,
      "time":"12:00:00"
   },
   {
      "day":"sunday",
      "date":"2012-03-06",
      "target":"clinnics",
      "id":4,
      "status":0,
      "time":"00:00:00"
   },
   ...
]
2
. The behaviour of these constants is described on the JSON constants page.

Mã hóa bị ảnh hưởng bởi
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
4 được cung cấp và ngoài ra, việc mã hóa các giá trị phao phụ thuộc vào giá trị của serialize_precision.

Thông số

class Visits_Model_Visit implements JsonSerializable { ... // Only put properties here that you want serialized. public function jsonSerialize() { return Array( 'day' => $this->day, 'date' => $this->date, 'target' => $this->target, 'id' => $this->id, 'status' => $this->status, 'obj' => $this->obj->jsonSerialize(), // example for other objects 'time' => $this->time ); } ... } 5

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
5 được mã hóa. Có thể là bất kỳ loại ngoại trừ tài nguyên.string on success or
[
   {
      "day":"sunday",
      "date":"2012-03-06",
      "target":"\u0634\u0633\u064a",
      "id":1,
      "status":0,
      "time":"12:00:00"
   },
   {
      "day":"sunday",
      "date":"2012-03-06",
      "target":"clinnics",
      "id":4,
      "status":0,
      "time":"00:00:00"
   },
   ...
]
4
on failure.

Tất cả dữ liệu chuỗi phải được mã hóa UTF-8.

Ghi chú:Hàm json_encode () được sử dụng để mã hóa một giá trị cho định dạng JSON.
7.3.0 Bạn có thể JSON mã hóa một đối tượng trong PHP không?
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
4 was added.
7.2.0 Để mã hóa các đối tượng thành chuỗi được định dạng JSON trong PHP, bạn có thể sử dụng hàm json_encode (giá trị, tùy chọn, độ sâu). Tham số đầu tiên chỉ định đối tượng PHP để mã hóa. Bạn có thể kiểm soát cách đối tượng PHP sẽ được mã hóa thành JSON bằng cách chuyển kết hợp bitmasks trong tham số thứ hai., and
    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
4
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
4 were added.
7.1.0 Sự khác biệt giữa json_encode và json_decode là gì?
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
4 was added.
7.1.0 Tôi nghĩ rằng json_encode đảm bảo rằng PHP có thể đọc tệp .json nhưng bạn phải chỉ định một tên biến, trong khi với JSON_DECODE, nó giống nhau nhưng bạn phải chỉ định tên tệp.float values.

JSON_ENCODE trở lại là gì?

Hàm json_encode () có thể trả về một chuỗi chứa biểu diễn JSON của giá trị được cung cấp. Mã hóa bị ảnh hưởng bởi các tùy chọn được cung cấp, và ngoài ra, việc mã hóa các giá trị phao phụ thuộc vào giá trị của serialize_precision.json_encode() example

{"a":1,"b":2,"c":3,"d":4,"e":5}
2

{"a":1,"b":2,"c":3,"d":4,"e":5}
3

{"a":1,"b":2,"c":3,"d":4,"e":5}
4

(Php 5> = 5.2.0, Php 7, Php 8, Pecl Json> = 1.2.0)

{"a":1,"b":2,"c":3,"d":4,"e":5}

JSON_ENCODE - Trả về biểu diễn JSON của một giá trịjson_encode() example showing some flags in use

{"a":1,"b":2,"c":3,"d":4,"e":5}
5

{"a":1,"b":2,"c":3,"d":4,"e":5}
3

{"a":1,"b":2,"c":3,"d":4,"e":5}
7

(Php 5> = 5.2.0, Php 7, Php 8, Pecl Json> = 1.2.0)

Normal: ["","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}

JSON_ENCODE - Trả về biểu diễn JSON của một giá trị

{"a":1,"b":2,"c":3,"d":4,"e":5}
8

Sự mô tả

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

JSON_ENCODE (hỗn hợp

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
1, int
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
2 = 0, int
class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
3 = 512): Chuỗi | Sai

{"a":1,"b":2,"c":3,"d":4,"e":5}
9

{"a":1,"b":2,"c":3,"d":4,"e":5}
3

Normal: ["","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}
1

(Php 5> = 5.2.0, Php 7, Php 8, Pecl Json> = 1.2.0)

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

JSON_ENCODE - Trả về biểu diễn JSON của một giá trị

    ...
    $entry = new Visits_Model_Visit();
    $entry->setId($row->visit_id)
          ->setDay($row->day)
          ->setDate($row->date)
          ->setTarget($row->target)
          ->setStatus($row->visit_status)
          ->setTime($row->visit_time);

    $visitsEntries[] = $entry->jsonSerialize();
    ...
7 option example

Normal: ["","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}
3

(Php 5> = 5.2.0, Php 7, Php 8, Pecl Json> = 1.2.0)

string(4) "12.0"
string(2) "12"

JSON_ENCODE - Trả về biểu diễn JSON của một giá trị

JSON_ENCODE - Trả về biểu diễn JSON của một giá trị:

Sự mô tảjson_last_error() can be used to determine the exact nature of the error.

JSON_ENCODE - Trả về biểu diễn JSON của một giá trị:

Sự mô tả

JSON_ENCODE - Trả về biểu diễn JSON của một giá trị:

Sự mô tảjson_encode() will generate JSON that is a simple value (that is, neither an object nor an array) if given a string, int, float or bool as an input

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
5. While most decoders will accept these values as valid JSON, some may not, as the specification is ambiguous on this point.

Để tóm tắt, luôn luôn kiểm tra rằng bộ giải mã JSON của bạn có thể xử lý đầu ra bạn tạo từ json_encode ().json_encode().

Xem thêm

  • Jsonserializable
  • json_decode () - giải mã chuỗi JSON
  • json_last_error () - Trả về lỗi cuối cùng xảy ra
  • serialize () - tạo biểu diễn có thể lưu trữ của một giá trị

Bohwaz ¶

10 năm trước

Normal: ["","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}
5

Normal: ["","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}
6

Normal: ["","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}
7

Normal: ["","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}
8

Normal: ["","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}
9

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"]"
0

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"]"
1

Ryan tại Ryanparman dot com

12 năm trước

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"]"
2

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"]"
3

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"]"
4

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"]"
1

guilhenfsu tại gmail dot com ¶

9 năm trước

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"]"
6

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

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"]"
8

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"]"
9

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"}"
0

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"}"
1

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"}"
2

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"]"
1

CK tại Ergovia dot de ¶

9 năm trước

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"}"
4

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"}"
5

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"}"
6

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

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"}"
8

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"}"
9

string(4) "12.0"
string(2) "12"
0

string(4) "12.0"
string(2) "12"
1

string(4) "12.0"
string(2) "12"
2

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"]"
1

CK tại Ergovia dot de ¶

Istratov vadim ¶

string(4) "12.0"
string(2) "12"
4

string(4) "12.0"
string(2) "12"
5

string(4) "12.0"
string(2) "12"
6

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"]"
1

13 năm trước

Nick ¶

string(4) "12.0"
string(2) "12"
8

string(4) "12.0"
string(2) "12"
9

json_encode()0

json_encode()1

json_encode()2

json_encode()3

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"]"
1

6 năm trước

Nick ¶

json_encode()5

json_encode()6

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"]"
1

6 năm trước

Istratov vadim ¶

json_encode()8

json_encode()9

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
00

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
01

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
02

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
03

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"]"
1

13 năm trước

Istratov vadim ¶

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
05

13 năm trước

Nick ¶

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
06

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
07

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
08

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
09

6 năm trước

Walter Tross ¶

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
10

Garrett ¶

Sam Barnum ¶

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
11

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
12

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"]"
1

pvl dot kolensikov tại gmail dot com ¶

11 năm trước

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
14

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
15

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
16

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
17

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"]"
1

spam.goes.in.here tại gmail.com ¶

Sam Barnum ¶

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
19

class Visits_Model_Visit implements JsonSerializable {
    ...
    // Only put properties here that you want serialized.
    public function jsonSerialize() {
        return Array(
           'day'    => $this->day,
           'date'   => $this->date,
           'target' => $this->target,
           'id'     => $this->id,
           'status' => $this->status,
           'obj'    => $this->obj->jsonSerialize(), // example for other objects
           'time'   => $this->time
        );
    }
    ...
}
20

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"]"
1

Hàm PHP json_encode () làm gì?

Hàm json_encode () được sử dụng để mã hóa một giá trị cho định dạng JSON.encode a value to JSON format.

Bạn có thể JSON mã hóa một đối tượng trong PHP không?

Để mã hóa các đối tượng thành chuỗi được định dạng JSON trong PHP, bạn có thể sử dụng hàm json_encode (giá trị, tùy chọn, độ sâu).Tham số đầu tiên chỉ định đối tượng PHP để mã hóa.Bạn có thể kiểm soát cách đối tượng PHP sẽ được mã hóa thành JSON bằng cách chuyển kết hợp bitmasks trong tham số thứ hai.you can use the json_encode(value, options, depth) function. The first parameter specifies the PHP object to encode. You can control how the PHP object will be encoded into JSON by passing a combination of bitmasks in the second parameter.

Sự khác biệt giữa json_encode và json_decode là gì?

Tôi nghĩ rằng json_encode đảm bảo rằng PHP có thể đọc tệp .json nhưng bạn phải chỉ định một tên biến, trong khi với JSON_DECODE, nó giống nhau nhưng bạn phải chỉ định tên tệp.json_encode makes sure that php can read the . json file but you have to specify a variable name, whereas with json_decode it's the same but you have to specify a file name.

JSON_ENCODE trở lại là gì?

Hàm json_encode () có thể trả về một chuỗi chứa biểu diễn JSON của giá trị được cung cấp.Mã hóa bị ảnh hưởng bởi các tùy chọn được cung cấp, và ngoài ra, việc mã hóa các giá trị phao phụ thuộc vào giá trị của serialize_precision.a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.