title: How to convert object to JSON in symfony2
body: <p>I am using this:</p>
  
  <pre><code>    $users = $em-&gt;getRepository('UserBundle:User')-&gt;getallUsers($search);
      $response = new Response(json_encode($users));
      $response-&gt;headers-&gt;set('Content-Type', 'application/json');
      return $response;
  </code></pre>
  
  <p>Users are multiple entities not single result.</p>
  
  <p>But I am getting this:</p>
  
  <pre><code>[{},{},{},{},{},{}]
  </code></pre>
  
  <p>I want something like:</p>
  
  <pre><code>[ { label: $user.getName(), value: $user.getId() }, ... ]
  </code></pre>
  
  <p>How can i do that?</p>
  
  <p><strong>EDIT:</strong> I also tried <code>json_encode($users-&gt;toArray())</code> then I get this error:</p>
  
  <blockquote>
    <p>Call to a member function <code>toArray()</code> on a non-object</p>
  </blockquote>