如何解析php中的错误json?

时间:2019-01-19 15:21:27

标签: php json parsing

我正在尝试解析下面的json,但我不能,结果一直为空。

这是json:

$response = '{"Status":1,"CodigoErro":null,"SKYException":"","ResultString":"{\"DadosBasicos\":{\"NomeCompleto\":null,\"Nome\":null,\"Sobrenome\":null,\"DataNascimento\":\"0001-01-01T00:00:00\",\"EstadoCivil\":null,\"Login\":\"xxxxxxxxxxxxx@yahoo.com.br\",\"Sexo\":0,\"CPF_CNPJ\":\"09414954775\",\"IdPerfilFaturamento\":null,\"IdConta\":\"13879034\",\"Assinaturas\":[{\"Id\":\"62606412\",\"ParentId\":null,\"Email\":\"xxxxxxxxxxxxx@yahoo.com.br\",\"DNA\":\"010000000001001000000010000000001000000000000000001000000010000000000001000000000000000010110001111101000000001000000001000000000111101000000100000101000001000011111111111110000000000000000000011000000010101011101111110100000000111110101101111100010000000000011011111111011010011011100011111111111101111111111111111111111111111111111101111111111110101111111111111011110100000011111111111111111111000\",\"StatusAssinatura\":1,\"ValorEmAberto\":null,\"CEP\":\"27540440\",\"TipoAssinatura\":0,\"ModoEntrega\":0,\"Produtos\":[],\"Equipamentos\":[],\"IlhaMaster\":false,\"Tier1\":true,\"PrincipalId\":\"062606412\",\"Enderecos\":null,\"Telefones\":null,\"TipoOrganizacao\":0,\"PassoRegua\":null,\"GeoCode\":\"BRL04100\",\"Fidelizado\":false,\"PossuiInteratividadePrePago\":false,\"PossuiSKYDigitalCortesia\":false,\"PossuiCanaisCortesiaLivre\":false,\"DataAtivacao\":\"2011-01-25T00:00:00\",\"DataFidelizacao\":\"2012-01-25T00:00:00\",\"TipoCliente\":\"F\",\"Segmento\":null,\"Pacote\":null,\"GloboCongelada\":[],\"ListaDeCanais\":[],\"CodigoTipoAssinatura\":null,\"CanceladoAteSeisMeses\":false,\"DataCancelamento\":null}]},\"DadosComplementares\":null}","Token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjcGZfY25waiI6IjA5NDE0OTU0Nzc1Iiwic2lnbmF0dXJlcyI6WyI2MjYwNjQxMiJdLCJpYXQiOjE1NDc5MTA4NDgsImV4cCI6MTU0Nzk5NzI0OH0.ho1V67cI4jZPXZ-DqnF-WzCRSKSY38uJuQs0tRXrdYM"}';

这是我尝试做的事情:

$json_bad = json_decode($response);
$json_002 = json_decode($json_bad->ResultString);

$email = $json_002->Login;

但是,它不起作用,我不断得到空变量。

2 个答案:

答案 0 :(得分:1)

您的变量$json_002包含值为DadosBasicos的多级

因此,您可以通过两种方式访问​​它:

  1. 使用stdClass对象:

     $json_bad = json_decode($response);
     $json_002 = json_decode($json_bad->ResultString);
    
     $email = $json_002->DadosBasicos->Login;
    
  2. 使用数组:

     $json_bad = json_decode($response,true);
     $json_002 = json_decode($json_bad['ResultString'],true);
    
     $email = $json_002['DadosBasicos']['Login'];
    

答案 1 :(得分:0)

您在第二个JSON解码数据中缺少1级...

{"DadosBasicos":{"NomeCompl...

所以您的电子邮件地址实际上在

$email = $json_002->DadosBasicos->Login;