gpt4 book ai didi

PHP json_decode 返回空数组

转载 作者:行者123 更新时间:2023-12-03 01:59:08 31 4
gpt4 key购买 nike

我只是从 php 文档测试这个示例 ( http://au2.php.net/manual/en/function.json-decode.php )

这是我的代码:

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

但它只返回一个空数组。

不知道为什么......一直在寻找但没有找到解决方案。

请帮忙!

最佳答案

您可以在以下位置进行验证 网站:http://jsonlint.com/

您必须使用 php“json_decode()”函数来解码 json 编码数据。基本上 json_decode() 函数将 JSON 数据转换为 PHP 数组。

语法:json_decode(数据,dataTypeBoolean,深度,选项)

data : - 您要在 PHP 中解码的 json 数据。

dataTypeBoolean(可选):- bool 值,如果设置为“true”,则使函数返回 PHP 关联数组;如果省略此参数或将其设置为“false”,则返回 PHP stdClass 对象”。这两种数据类型都可以像数组一样访问,并使用基于数组的 PHP 循环进行解析。

深度:- 可选的递归限制。使用整数作为此参数的值。

选项:- 可选 JSON_BIGINT_AS_STRING 参数。

现在轮到你的代码了

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

将有效的 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/>";

}

关于PHP json_decode 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24848625/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com