-2

I´m using a ajax requisition to take data from a Controller. The controller returns a json encode with the this data:

[{"Cliente":{"id":"1","nome":"Andre Figueredo","nome_representante":"Andre Figuerdo","email":"avs.fox@gmail.com","empresa":"Fox","area_atuacao":"Mocuton"},"Telefone":{"id":"1","cliente_id":"1","telefone":"11 33613529","telefone_representante":"11 97666-9899"},"Endereco":{"id":"1","cliente_id":"1","logradouro":"Av Washington","numero":"109","complemento":"","bairro":"santa luzia","municipio":"sao paulo","estado":"sp","cep":"02802-120"}}]

How can i read this json?

thanks!

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Stark
  • 65
  • 2
  • 9

2 Answers2

3

if you want to read in view you can use

as per http://php.net/json_decode, you need to specify you want an associative array instead of an object from json_decode:

$obj = json_decode($jsondata,true);
echo "<pre>"
print_r($obj);

let me know if can help you further..

liyakat
  • 11,825
  • 2
  • 40
  • 46
  • Thanks for your helper, but i need to read on the jquery/javascript file. – Stark Jul 05 '13 at 11:23
  • 1
    @Stark Please use this answer to get response in js http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript – liyakat Jul 05 '13 at 12:45
  • let me know if i could help you more, And it would be glad if you will accept my answer, as it is useful to you so in future has someone with same issue which can utilize our answer. – liyakat Jul 05 '13 at 12:46
0

In your controller method encode and return the result of $content['Cliente'] instead of $content.

    $content = $this->Content->find('first', $options);
    return json_encode($content['Cliente']);

Now the result of json_encode will look like this which is appropriate for json_decode

  {"id":"1","nome":"Andre Figueredo","nome_representante":"Andre Figuerdo","email":"avs.fox@gmail.com","empresa":"Fox","area_atuacao":"Mocuton"},"Telefone":{"id":"1","cliente_id":"1","telefone":"11 33613529","telefone_representante":"11 97666-9899"},"Endereco":{"id":"1","cliente_id":"1","logradouro":"Av Washington","numero":"109","complemento":"","bairro":"santa luzia","municipio":"sao paulo","estado":"sp","cep":"02802-120"}
Thomas Rollet
  • 1,573
  • 4
  • 19
  • 33
Sujit
  • 61
  • 3
  • 7