PHP: json decoding restrictions

Take this code:

$json = file_get_contents($this->url, true); $decode = json_decode($json, true); foreach ($decode as $key => $value) { ... } 

Pretty simple, mm?

Passing $ json with up to 500 array elements .... working correctly!

Above this limit ... error:

Warning. Invalid argument provided by foreach () in /c/website/retriever/WsGlassRetriever.php on line 19

Is there a memory limit for this function argument?

I did not find anything in the docs. My version is PHP 5.2.17-rnx1.1 with Suhosin-Patch 0.9.7 (cli)

+6
source share
3 answers

json_decode returns NULL if there is an error in the JSON syntax. I just successfully tested an array of 1000 elements and it went fine.

Make sure your JSON is formatted correctly. Even minor ones like single quotation marks, not double quotes, or forgetting to put the property name in quotation marks or using a character outside the 32-127 range without proper encoding in UTF-8, can cause these problems.

+11
source

I am sure that your JSON code above 500 has a formatting problem, used JSON with more than 20,000 values, here is a simple script from an array of 2000

 $string = "Sample String Data ΒΆ"; $string = preg_replace( '/[^[:print:]]/', '',$string); // remove all values that can affect JSON $array = array(); for($i = 0 ; $i < 2000; $i++) { if(mt_rand(0, 1)) { $array[] = $string ; } else { $array[] = array($string,1,$string) ; } } $json = json_encode($array); $decodeArray = json_decode($json); switch (json_last_error()) { case JSON_ERROR_NONE: echo ' - No errors'; break; case JSON_ERROR_DEPTH: echo ' - Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: echo ' - Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: echo ' - Unexpected control character found'; break; case JSON_ERROR_SYNTAX: echo ' - Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: echo ' - Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: echo ' - Unknown error'; break; } echo "<br />" ; foreach ($decodeArray as $key => $value) { print_r($value) ; flush(); } 

Edit 2

I was interested to know if there are any restrictions .. just checked it with 250,000 (Two hundred and fifty thousand values ​​and it works fine)

Thanks Oleku

+5
source

In my case, JSON was right. My problem was the parameter "JSON_BIGINT_AS_STRING", which caused the error "Maximum stack depth".

 $jsonResult = json_decode($expr,true,JSON_BIGINT_AS_STRING); 

I removed the argument "JSON_BIGINT_AS_STRING" and the error went away:

 $jsonResult = json_decode($expr,true); 
0
source

Source: https://habr.com/ru/post/911860/


All Articles