简体   繁体   English

PHP json_decode返回空数组

[英]PHP json_decode return empty array

I just test this sample from php doc ( http://au2.php.net/manual/en/function.json-decode.php ) 我只是从php doc( http://au2.php.net/manual/en/function.json-decode.php )测试此示例

here is my code: 这是我的代码:

<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; echo json_decode($json, true), '<br />';?>

But it just returns an EMPTY array. 但是它只返回一个EMPTY数组。

Have no idea why...Been searching around but no solution found. 不知道为什么...曾经四处搜寻,但未找到解决方案。

PLEASE help! 请帮忙!

you should not use echo because it is an array. 您不应该使用echo,因为它是一个数组。 use print_r or var_dump .it works fine 使用print_r或var_dump。它工作正常

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
print_r(json_decode($json, true));

Output: 输出:

Array
(
   [a] => 1
   [b] => 2
   [c] => 3
   [d] => 4
   [e] => 5
)

You can validate at following website: http://jsonlint.com/ 您可以在以下网站上进行验证: http : //jsonlint.com/

You have to use a php "json_decode()" function to decode a json encoded data. 您必须使用php“ json_decode()”函数来解码json编码的数据。 Basically json_decode() function converts JSON data to a PHP array. 基本上json_decode()函数将JSON数据转换为PHP数组。

Syntax: json_decode( data, dataTypeBoolean, depth, options ) 语法:json_decode(data,dataTypeBoolean,depth,options)

data : - The json data that you want to decode in PHP. data :-您要在PHP中解码的json数据。

dataTypeBoolean(Optional) :- boolean that makes the function return a PHP Associative Array if set to "true", or return a PHP stdClass object if you omit this parameter or set it to "false". dataTypeBoolean(可选) :布尔值,使函数如果设置为“ true”,则返回PHP关联数组;如果省略此参数或将其设置为“ false”,则返回PHP stdClass对象。 Both data types can be accessed like an array and use array based PHP loops for parsing. 可以像访问数组一样访问这两种数据类型,并使用基于数组的PHP循环进行解析。

depth :- Optional recursion limit. depth :-可选的递归限制。 Use an integer as the value for this parameter. 使用整数作为此参数的值。

options :- Optional JSON_BIGINT_AS_STRING parameter. options :-可选的JSON_BIGINT_AS_STRING参数。

Now Comes to your Code 现在进入您的代码

$json_string = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ;

Assign a valid json data to a variable $json_string within single quot's ('') as json string already have double quots. 将有效的json数据分配给单引号('')中的变量$ json_string,因为json字符串已经有双引号了。

// here i am decoding a json string by using a php 'json_decode' function, as mentioned above & passing a true parameter to get a PHP associative array otherwise it will bydefault return a PHP std class objecy array.

$json_decoded_data = json_decode($json_string, true);

// just can check here your encoded array data.
// echo '<pre>';
// print_r($json_decoded_data);

// loop to extract data from an array
foreach ($json_decoded_data as $key => $value) {
    echo "$key | $value <br/>";

}

No, it doesn't return an empty array. 不,它不会返回空数组。

Printing an array with echo just prints a string "Array()" . 使用echo打印数组只会打印字符串"Array()"

Use print_r or var_dump to get the structure of the variable. 使用print_rvar_dump获取变量的结构。

In newer PHP it will also emit a notice when using echo on an array ("Array to string conversion"), so you shouldn't do it anyway. 在较新的PHP中,当在数组上使用echo (“数组到字符串转换”)时,它也会发出通知,因此无论如何您都不应这样做。 The manual you've mentioned changed to print_r . 您提到的手册已更改为print_r

It works fine, but you use wrong method to display array. 它工作正常,但是您使用了错误的方法来显示数组。

To display array you cannot use echo but you need to use var_dump 要显示数组,您不能使用echo但需要使用var_dump

It works fine as others mention, but when you print the array it is converted to string, which means only the string "Array" will be printed instead of the real array data. 就像其他人提到的那样,它可以正常工作,但是当您打印数组时,它将转换为字符串,这意味着将仅打印字符串“ Array”而不是实际的数组数据。 You should use print_r() , var_dump() , var_export() or something similar to debug arrays like this. 您应该使用print_r()var_dump() ,var_export()或类似调试数组的东西。

If you turn on notices you will see: 如果打开通知,您将看到:

PHP Notice:  Array to string conversion in ...

The example you linked uses also var_dump for the same reason. 出于相同的原因,链接的示例也使用var_dump。

var_dump have pretty print in php5.4 var_dump在php5.4中有漂亮的打印

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; 
var_dump( json_decode($json));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM