Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_decode returns NULL for a string variable

Tags:

json

php

I met a really weird problem on json_decode, with this code:

$url="http://localhost:8983/solr/db/select?wt=json&rows=1&q=94305";
$string=file_get_contents($url);
echo $string; echo '<br><br>';
$json=json_decode($string);
var_dump($json);

I got the following result:

{"responseHeader":{"status":0,"QTime":0,"params":{"q":"94305","wt":"json","rows":"1"}},"response":{"numFound":165,"start":0,"docs":[{"price":"","tags":"ATMs","phone_n":"","location":"37.42409897,-122.1709976 ","store":"Discover ATM","store_id":"478602","state":"CA","latitude":"37.42409897","address":"459 LAGUNITA","zipcode_n":"94305","longitude":"-122.1709976\r","url":"Discover_ATM_459_LAGUNITA_Stanford_CA_94305","city":"Stanford","category":"ATMs","text":["","CA","459 LAGUNITA","94305","Stanford"],"spell":["Discover ATM"]}]}}

NULL 

It seems that I cannot json_decode this string. However, when I do like this (copy the output of the string above and put it to $string directly):

$string='{"responseHeader":{"status":0,"QTime":0,"params":{"q":"94305","wt":"json","rows":"1"}},"response":{"numFound":165,"start":0,"docs":[{"price":"","tags":"ATMs","phone_n":"","location":"37.42409897,-122.1709976 ","store":"Discover ATM","store_id":"478602","state":"CA","latitude":"37.42409897","address":"459 LAGUNITA","zipcode_n":"94305","longitude":"-122.1709976\r","url":"Discover_ATM_459_LAGUNITA_Stanford_CA_94305","city":"Stanford","category":"ATMs","text":["","CA","459 LAGUNITA","94305","Stanford"],"spell":["Discover ATM"]}]}}';
$json=json_decode($string);
var_dump($json);

json_decode works. Why json_decode get NULL at the first part while works properly here?

like image 857
user570494 Avatar asked Dec 19 '12 07:12

user570494


1 Answers

Your code looks okay, so let's take one step closer and investigate what $output really is. It helps to choose a representation that can handle ASCII ranges that you can't see.

echo bin2hex($output);

That will give a huge string, but you'll be mostly interested in the front and back of the string.

If that seems kosher, you can create an in-between representation:

echo preg_replace('@[\x00-\x1f\x7f-\xff]@e', '" (0x" . dechex(ord("\\0")) . ") "', $output);

It replaces any character in the lower or higher ASCII range with a hexadecimal representation, making it somewhat easier to spot them :)

Update

From your investigation based on the above, your string seems to contain a carriage return - \r - somewhere in the middle.

"CA","latitude":"37.42409897","
                            ^

You can remove those with a preg_replace() if it can't be solved in another way.

preg_replace("/\r(?!\n)/", '', $output);

That removes any \r not followed by a \n.

like image 200
Ja͢ck Avatar answered Nov 15 '22 09:11

Ja͢ck