PHP: json decode limits

匿名 (未验证) 提交于 2019-12-03 00:48:01

问题:

Take this code:

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

Pretty simple, uh?

Passing a $json with up to 500 array elements.... works right!

Above that limit... the error is:

Warning: Invalid argument supplied for foreach() in /c/website/retriever/WsGlassRetriever.php on line 19

Is there some memory limit for that function's argument?

I didn't found nothing about it in the docs. My version is PHP 5.2.17-rnx1.1 with Suhosin-Patch 0.9.7 (cli)

回答1:

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

Double-check that your JSON is correctly formatted. Even something as small as having single quotes instead of double, or forgetting to put a property name in quotes, or using a character outside the 32-127 range without correctly encoding it in UTF-8 can cause these problems.



回答2:

Am sure your JSON code above 500 has a formatting issue , have used JSON with over 20,000 values here is a simple script of 2000 array

Edit 2

I was so interested to know if there is any limitation .. just tested it with 250,000 (Two hundred and fifty thousand values and it works fine )

Thanks Oleku



回答3:

In my case the JSON was correct. My problem was the "JSON_BIGINT_AS_STRING" parameter that was causing the error "Maximum stack depth exceeded".

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

I removed the "JSON_BIGINT_AS_STRING" argument and the error is gone:

$jsonResult = json_decode($expr,true); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!