Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_decode() returns blank but it's valid json

Tags:

json

php

$a = '[{"function":"error_handler","class":"LP","type":"::","args":[256,"Call to undefined method LP_pdo::get_rorzxx() on D:\\MARK\\htdocs\\lessphptest\\application\\controllers\\users.php (23)","D:\\MARK\\htdocs\\lessphp\\LP.php",210,{"e":{"type":1,"message":"Call to undefined method LP_pdo::get_rorzxx()","file":"D:\\MARK\\htdocs\\lessphptest\\application\\controllers\\users.php","line":23}}]},{"file":"D:\\MARK\\htdocs\\lessphp\\LP.php","line":210,"function":"trigger_error","args":["Call to undefined method LP_pdo::get_rorzxx() on D:\\MARK\\htdocs\\lessphptest\\application\\controllers\\users.php (23)",256]},{"function":"shutdown","class":"LP","type":"::","args":[]}]';
$a = json_decode($a);
print_r($a);

echo json_last_error();

print_r() returns blank.

json_last_error() returns 4 which is JSON_ERROR_SYNTAX

But, when I run the json string in http://jsonlint.com/ it returns Valid JSON

Any ideas why?

like image 323
IMB Avatar asked Jul 31 '12 11:07

IMB


People also ask

What does json_decode return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

How do you check a JSON is valid or not?

The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.

What is the use of json_decode in PHP?

The json_decode() function is used to decode or convert a JSON object to a PHP object.

Does JSON use null or none?

JSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types.


1 Answers

You need to escape your \ once for PHP and once again for JSON

D:\\\\....

like image 157
Anirudh Ramanathan Avatar answered Sep 30 '22 13:09

Anirudh Ramanathan