php json_decode() 期望参数 1 是字符串,给定数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31050227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
json_decode() expects parameter 1 to be string, array given?
提问by mparo_afridi
this is my data ,After Json_encode()
这是我的数据,之后 Json_encode()
Array
(
[{"customerId":"1","customer_name":"Jon_doe","amount":"12312312","billcode":"b1231","billname":"cashbilname","billcategorycode":"1234","billcategory":"utility","month":"May","year":"2015","txcode":"10","stationid":"152","station":"Coroom","operatorcode":"1200","operator":"jame","terminal":"ter12312","txdate":"12\/2\/2015","txtime":"12:21:22_PM"}]
=>
)
now i want to decode it back ,by applying json_decode()
it gives the following error
现在我想解码它,通过应用json_decode()
它会出现以下错误
json_decode() expects parameter 1 to be string, array given
json_decode() 期望参数 1 是字符串,给定数组
any idea sugestion what to do ?
任何想法sugestion怎么办?
回答by Fabien
Your json
must be in string
, not in array
你json
必须在string
,而不是在array
$json_string = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$json_array = json_decode($json_string);
$json_array : array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
If your json
is in array
you can do :
如果您json
在,array
您可以:
$json_string_in_array = array('{"a":1,"b":2,"c":3,"d":4,"e":5}');
$json_array = json_decode($json_string_in_array[0]);
回答by BBBThunda
json_encode() returns a string, so I don't know how you can be getting an array out of it unless you are storing it in an array yourself like:
json_encode() 返回一个字符串,所以我不知道如何从中获取数组,除非您自己将其存储在数组中,例如:
$json = [];
$json[] = json_encode($someArray);
Instead, just store it in a non-array variable:
相反,只需将其存储在非数组变量中:
$jsonString = json_encode($someArray);
Then you can decode it like this:
然后你可以像这样解码它:
$decodedArray = json_decode($jsonString);
回答by PHPJungle
$jsonstr = '[{"customerId":"1","customer_name":"Jon_doe","amount":"12312312","billcode":"b1231","billname":"cashbilname","billcategorycode":"1234","billcategory":"utility","month":"May","year":"2015","txcode":"10","stationid":"152","station":"Coroom","operatorcode":"1200","operator":"jame","terminal":"ter12312","txdate":"12\/2\/2015","txtime":"12:21:22_PM"}]';
$ar = json_decode($jsonstr,true); # json string to Array
$obj = json_decode($jsonstr); # json string to Object
var_dump($ar,$obj);