how to parse a json return with complex object

Asked

Viewed 247 times

2

How do I parse a return json where I have optional fields, such as phone, the user can have more than one as residential, mobile and phone to message, in this case if the user informed the phone he lists if I have not informed he does not list any or only one or two, follows the example of a return below.

{
     "transacao":{
        "codigo":1,
        "nome":"bruno",
        "sobrenome":"richart",
        "idade":28,
        "endereco":{
           "rua":"Avenidas das Americas",
           "numero":12300,
           "bairro":"Barra da Tijuca",
           "cidade":"Andradina",
           "uf":"rj"
        },
        "telefone":[
           {
              "tipo":"residencial",
              "ddd":18,
              "numero":37236207
           },
           {
              "tipo":"celular",
              "ddd":18,
              "numero":972770022
           }
        ],
        "compra":{
           "idproduto":23,
           "descricao":"Celular Iphone",
           "quantidade":1
        }
     }
  }
  • When he doesn’t have a phone he doesn’t even have that key? Is that it? Do you want to know when you have that key or not?

  • 1

    @Virgilionovic Yes, in case the phone would be an array, it could have up to 3 phones, but the system would only do the parser if there was at least one phone.

  • Possible duplicate of http://answall.com/questions/4410/received-data

2 answers

2

Just use json_decode($json,true) this way you will create an associative array.

Example:

<?php
        $json = '{
     "transacao":{
        "codigo":1,
        "nome":"bruno",
        "sobrenome":"richart",
        "idade":28,
        "endereco":{
           "rua":"Avenidas das Americas",
           "numero":12300,
           "bairro":"Barra da Tijuca",
           "cidade":"Andradina",
           "uf":"rj"
        },
        "telefone":[
           {
              "tipo":"residencial",
              "ddd":18,
              "numero":37236207
           },
           {
              "tipo":"celular",
              "ddd":18,
              "numero":972770022
           }
        ],
        "compra":{
           "idproduto":23,
           "descricao":"Celular Iphone",
           "quantidade":1
        }
     }
  }';


$arr = json_decode($json,true);

foreach($arr['transacao']['telefone'] as $telefone){
    print_r($telefone);
}

See working in : Phpsandbox

  • worked perfectly thank you, I was only in doubt if I have no phone, there is some function to check and treat the error?

  • 1

    @Brunorichart if there is no phone inside the object ['telefone'] will not enter the Loop, you just have to make sure that this object will come (even if empty), if you cannot guarantee this, check before with isset($arr['transacao']['telefone']) if it exists it will return true

2


To identify a phone key on this one array would be:

//cria um array associativo quando segundo paramentro é true
$array = json_decode($json, true); 
//retorna true se a chave existir
array_key_exists('telefone', $array['transacao']);

the code:

$array = json_decode($json, true);
if (array_key_exists('telefone', $array['transacao']))
{
    foreach($arr['transacao']['telefone'] as $telefone)
    {
        echo $telefone['tipo'];
        echo $telefone['ddd'];
        echo $telefone['numero'];           
    }
}

References:

it avoids making mistakes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.