json_decode()期望参数1是字符串,给定数组?

时间:2015-06-25 12:20:56

标签: php arrays json

这是我的数据,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"}] 
    => 
        )

现在我想要解码它,通过应用json_decode()它会出现以下错误

  

json_decode()期望参数1为字符串,给定数组

有什么想法可以做什么?

3 个答案:

答案 0 :(得分:5)

您的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);

如果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]);

答案 1 :(得分:2)

json_encode()返回一个字符串,所以我不知道如何从中获取数组,除非你自己将它存储在数组中:

$json = [];
$json[] = json_encode($someArray);

相反,只需将其存储在非数组变量中:

$jsonString = json_encode($someArray);

然后你可以像这样解码它:

$decodedArray = json_decode($jsonString);

答案 2 :(得分:0)

SCNu32