javascript - 返回值显示为字符串而不是 json

标签 javascript php json codeigniter

我正在使用文件获取内容方法来获取统计数据,如下所示

$val = file_get_contents('https://example.com/getStatsData');

我得到的输出值如下

{"msg":"success","data":[[{"stats_abc":"1223","stats_bcd":"55684","stats_cda":"14999","stats_def":232456,"stats_efg":"432868","stats_fgh":"7558","stats_ghi":"3,778.72","stats_date":"20-01-2018 17:00 PM"}],null]}

但是当我将其解码为 json_decode($val) 时。它没有显示任何内容。 谁能帮助我将上述数据作为数组/对象获取?

提前致谢

最佳答案

这确实有效:

<?php

$json = '{
  "msg": "success",
  "data": [
    [{
      "stats_abc": "1223",
      "stats_bcd": "55684",
      "stats_cda": "14999",
      "stats_def": 232456,
      "stats_efg": "432868",
      "stats_fgh": "7558",
      "stats_ghi": "3,778.72",
      "stats_date": "20-01-2018 17:00 PM"
    }], null
  ]
}';

echo '<pre>';
print_r(json_decode($json));
echo '</pre>';

结果:

stdClass Object
(
    [msg] => success
    [data] => Array
        (
            [0] => Array
                (
                    [0] => stdClass Object
                        (
                            [stats_abc] => 1223
                            [stats_bcd] => 55684
                            [stats_cda] => 14999
                            [stats_def] => 232456
                            [stats_efg] => 432868
                            [stats_fgh] => 7558
                            [stats_ghi] => 3,778.72
                            [stats_date] => 20-01-2018 17:00 PM
                        )
                )
            [1] => 
        )
)

只是为了表明 json 本身没问题。

关于javascript - 返回值显示为字符串而不是 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48355886/

相关文章:

php - CakePHP 在基本路径 (/) 中路由命名参数

java - 如何在 Spring 中从文件连接 JSON 数据?

javascript - 如何检测是否有水平滚动条?

javascript - grunt-contrib-clean 不删除单个文件

php - 如何实现可以处理用户输入的服务器端应用程序?

PHP 文件包含

javascript - 从 JSON url 读取内容并写出这些内容

json - Postgres 多对多 JSON 聚合

javascript - while with Promise 怎么办?

javascript - 表单提交后如何保持动态下拉菜单的值保持选中状态?