0

I have a list of Object DTO's for which I want to convert to JSON using json_encode

Object: CouponDTO.php

class CouponDTO
{
    private $id;
    private $title;
}

I have an array of objects, now I need to convert that array of objects to JSON, I tried json_encode, but it provided below empty result.

[
    {},
    {},
    {},
    {},
    {}
]

Below is the code to create array of objects:

$couponDTOs = array();
for($i = 0; $i < 5; $i++) {
    $couponDTO = new CouponDTO();
    $couponDTO->setId("1");
    $couponDTO->setTitle("title");
    array_push($couponDTOs, $couponDTO);
}

I am expecting something like this:

[
    {
        "id": 1,
        "title": title
    },
    {
        "id": 1,
        "title": title
    },
    {
        "id": 1,
        "title": title
    },
    {
        "id": 1,
        "title": title
    },
    {
        "id": 1,
        "title": title
    },
]
Sharad Soni
  • 378
  • 1
  • 5
  • 18

0 Answers0