PHP Json_Decode() Returns Null With Valid Json

PHP json_decode() returns NULL with valid JSON?

It could be the encoding of the special characters. You could ask json_last_error() to get definite information.

Update: The issue is solved, look at the "Solution" paragraph in the question.

Php - json_decode returns null

<?php
$data='{
"id": "1",
"fields": [{
"id": "1",
"value": "asdasd"
}, {
"id": "2",
"value": "asdasd"
}, {
"id": "3",
"value": "asdasd"
}]
}';

$dataNew=json_decode($data,true);
echo '<pre>';
print_r($dataNew);

Your json was not valid. The json-keys need to be inside double-quotes. After that json_decode will work fine.

Output is:

Array
(
[id] => 1
[fields] => Array
(
[0] => Array
(
[id] => 1
[value] => asdasd
)

[1] => Array
(
[id] => 2
[value] => asdasd
)

[2] => Array
(
[id] => 3
[value] => asdasd
)

)

)

Json to php, json_decode returns NULL

Please use json_decode(); on the raw JSON data. As long as it is valid JSON you don't need any extra code to be able to access this smoothly.

Access the returned value as an array.

$arr = json_decode(file_get_contents($json));

json_decode returns NULL after webservice call

EDIT:
Just did some quick inspection of the string provided by the OP. The small "character" in front of the curly brace is a UTF-8 B(yte) O(rder) M(ark) 0xEF 0xBB 0xBF. I don't know why this byte sequence is displayed as  here.

Essentially the system you aquire the data from sends it encoded in UTF-8 with a BOM preceding the data. You should remove the first three bytes from the string before you throw it into json_decode() (a substr($string, 3) will do).

string(62) "{"action":"set","user":"123123123123","status":"OK"}"
^
|
This is the UTF-8 BOM

As Kuroki Kaze discovered, this character surely is the reason why json_decode fails. The string in its given form is not correctly a JSON formated structure (see RFC 4627)

String appears to be valid JSON, but `json_decode()` returns NULL

The response Google is giving you isn't valid JSON because the labels are not quoted. You'll have to parse it yourself.

$response = '{lhs: "555 Euros",rhs: "796.64700 U.S. dollars",error: "",icc: true';
preg_match('/rhs:\s*"([^"]+)"/', $response, $m);
echo $m[1];

Output:

796.64700 U.S. dollars

json_decode() returns null, even though file_get_contents() works perfectly, even though json is valid

You can do it like:

//storing json contents in a variable.
$json_contents = file_get_contents('../../Datafiles/allThreads.txt');

//decode json
$currentThreadasList = json_decode($json_contents, TRUE)

Edit-1:

Have you tried the following:

$currentThreadasList = json_decode(file_get_contents('../../Datafiles/allThreads.txt'), TRUE);

json_decode() returns null issues

What a HORRENDOUS debug session.. well there's good news.. I figured it out..

I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle ", which is what PHPUnit sends back (Come on Sebastian!), so to fix it:

$json = str_replace('"', '"', $json);

Now I thought they were the same.. maybe someone can enlighten me..



Related Topics



Leave a reply



Submit