json_decode returns null

3

I have a JSON file that is in this link . I need to get and display the data in PHP, I have the following code to test:

$linkAnapro = 'http://s.anapro.com.br/a-p/dados/b3gkhu%252f3ohE%253d/estoque.json';

$output = file_get_contents($linkAnapro);
$json = json_decode($output);

print_r($json);

The value it returns is NULL , if I do print_r($output) it returns the JSON. What could be happening?

    
asked by anonymous 06.04.2015 / 19:48

3 answers

5

The http://s.anapro.com.br/a-p/dados/b3gkhu%252f3ohE%253d/estoque.json url is returning characters in content compatible with iso-8859-1/windows-1252 , to use json_decode it is necessary to use utf8_encode first:

<?php
$linkAnapro = 'http://s.anapro.com.br/a-p/dados/b3gkhu%252f3ohE%253d/estoque.json';

$output = file_get_contents($linkAnapro);
$output = utf8_encode($output);
$json = json_decode($output);

print_r($json);
  

Note: This function will return false if the JSON encoded data has more than 127 elements.

Note that json_decode returns a stdClass and that they will be in utf-8 to convert to array use so json_encode(..., true) and if you will use iso-8859-1 (or compatible) on your page it will be you need a recursive function with utf8_decode to not show those "strange" characters, here is an example of how to do:

<?php
$linkAnapro = 'http://s.anapro.com.br/a-p/dados/b3gkhu%252f3ohE%253d/estoque.json';

$output = file_get_contents($linkAnapro);
$output = utf8_encode($output);
$json = json_decode($output, true);

function utf8_decode_recursive(&$val, $key){
    $val = utf8_decode($val);
}

array_walk_recursive($json, 'utf8_decode_recursive');

print_r($json);
    
06.04.2015 / 20:04
1

The json_decode fails because the value it receives contains unicode characters, probably BOM B and O rder M ark).

One way to get around this is to use the utf8_encode function before calling < a href="http://php.net/manual/en_US/function.json-decode.php"> json_decode .

$linkAnapro = 'http://s.anapro.com.br/a-p/dados/b3gkhu%252f3ohE%253d/estoque.json';
$output = file_get_contents($linkAnapro);

$output = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($output));
$json = json_decode($output);

if (json_last_error() == 0) { // Sem erros
    print_r($json);
} else {
    echo "Erro inesperado ". json_last_error();
}

The function json_last_error() in this case returns JSON_ERROR_UTF8 , if you prefer to handle this error or others that may occur, do the following:

function json_decode2($valor){
$json = json_decode($valor);

switch (json_last_error()) {
    case JSON_ERROR_NONE:
        return $json;
    case JSON_ERROR_DEPTH:
        return 'A profundidade máxima da pilha foi excedida';
    case JSON_ERROR_STATE_MISMATCH:
        return 'JSON inválido ou mal formado';
    case JSON_ERROR_CTRL_CHAR:
        return 'Erro de caractere de controle, possivelmente codificado incorretamente';
    case JSON_ERROR_SYNTAX:
        return 'Erro de sintaxe';
    case JSON_ERROR_UTF8:  // O seu caso!
        $json = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($valor));
        return json_encode($json);
    default:
        return 'Erro desconhecido';
    }   
}

To use:

$linkAnapro = 'http://s.anapro.com.br/a-p/dados/b3gkhu%252f3ohE%253d/estoque.json';
$output = file_get_contents($linkAnapro);

echo json_decode2($output);

Another way would be to remove the characters that are causing the problem:

$linkAnapro = 'http://s.anapro.com.br/a-p/dados/b3gkhu%252f3ohE%253d/estoque.json';

$output = file_get_contents($linkAnapro);
$json = json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $output)); // Remove os caracteres não imprimíveis da string

if (json_last_error() == 0) { // Sem erros
    print_r($json);
} else {
    echo "Erro inesperado ". json_last_error();
}
    
06.04.2015 / 20:09
0

In my case this method helped me json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $output));

    
13.08.2015 / 19:25