如何访问从php转换为javascript对象的实体

I am throwing entity object to javascript by ajax

in php

// obtaining object by doctrine2
$lesson = $em->getRepository('Lesson')
    ->findOneBy(array('id' => $myId));

// $lesson->getName(); // I can access the entity property here like this.

//throwing object as json
$response = array("responseCode" => 200,'lesson' => $lesson,"success" => true);
return new Response(json_encode($response),200,array('Content-Type'=>'application/json'));

in javascript

function reloadItems(){
    var url= "http://myajax.com/ajax/";
    $.post(url,something,function(data){
// I got lesson object from php as json here.

// however these doesn't work
//      console.log(data.lesson.name);
//      console.log(data.lesson.getName);

    });

How can I get the each repository data in javascript??

When passed an object, json_encode can only grab the public properties. Methods are ignored completely. Since $lesson is a doctrine entity then I'm sure Lesson::name is either private or protected.

There are some serializer tools like http://symfony.com/doc/current/components/serializer.html which use reflection to access private/protected variables.

Or you can use PHP's (>=5.4) json serializer http://php.net/manual/en/jsonserializable.jsonserialize.php

Something like:

class Lesson implements \JsonSerializable
{
  private $name;

  public function jsonSerialize() 
  {
    return 
    [
      'name' => $this->name,
    ];
  }
}