无法从Json中提取数据

时间:2017-11-13 09:17:22

标签: php json extract

我无法从api中提取json中的数据。 我试了几个小时,尝试了各种格式。读取像How do I extract data from JSON with PHP?这样的Stackoverflow线程,但我无法看到我做错了什么。

这是到目前为止的代码:

$api_results = '{"status":"0000","data":{"opening_price":"6998000","closing_price":"7270000","min_price":"6750000","max_price":"7997000","average_price":"7188302.5804","units_traded":"78484.9241002","volume_1day":"78484.9241002","volume_7day":"335611.84181738","buy_price":"7268000","sell_price":"7274000","date":"1510563513031"}}';

$results = json_decode($api_results, true);

// Some variations I tried:
var_dump($results->status[1]);
var_dump($results->data[1]->opening_price);
var_dump($results["data"][1]["opening_price"]);

结束结果:NULL NULL NULL

我做错了什么?

感谢您的回答!我会赞成工作的。似乎我在格式化方面感到困惑!

6 个答案:

答案 0 :(得分:4)

<?php

$api_results = '{"status":"0000","data":{"opening_price":"6998000","closing_price":"7270000","min_price":"6750000","max_price":"7997000","average_price":"7188302.5804","units_traded":"78484.9241002","volume_1day":"78484.9241002","volume_7day":"335611.84181738","buy_price":"7268000","sell_price":"7274000","date":"1510563513031"}}';


$results = json_decode($api_results, true);
print_r($results['status']);
echo "</br>";
print_r($results['data']['opening_price']);

尝试以这种方式访问​​您的阵列。

输出结果为:

0000
6998000

密切关注嵌套数组。您需要先访问其父数组才能获取其值。

答案 1 :(得分:2)

json_decode移除true,这样您就会有像Demo

这样的对象结果
$results = json_decode($api_results);
var_dump($results->status);
var_dump($results->data->opening_price);

当您使用json_decode时,返回的对象将转换为关联数组。

答案 2 :(得分:2)

您是否阅读了json_decode()的文档(或者至少是您所链接问题的已接受答案)?如果将TRUE作为第二个参数传递给json_decode()(并且没有合理的理由不通过它),那么它会将JSON解码为关联arrays而不是对象。

可以使用the square bracket syntax访问PHP数组中的元素。

print_r($results)的简单调用告诉其结构:

Array
(
    [status] => 0000
    [data] => Array
        (
            [opening_price] => 6998000
            [closing_price] => 7270000
            [min_price] => 6750000
            [max_price] => 7997000
            [average_price] => 7188302.5804
            [units_traded] => 78484.9241002
            [volume_1day] => 78484.9241002
            [volume_7day] => 335611.84181738
            [buy_price] => 7268000
            [sell_price] => 7274000
            [date] => 1510563513031
        )
)

现在,访问它的项目是小菜一碟:

echo($results['status']);
# 0000
echo($results['data']['opening_price']);
# 6998000

答案 3 :(得分:0)

从json_decode中删除true并尝试这样的事情:

var_dump($results->status);
var_dump($results->data->opening_price);

如果您看到{}它正在引用对象而[]则表示它是一个数组。您试图将所有内容展示为数组

答案 4 :(得分:0)

使用此代码就像我认为工作正常..

<?php
$api_results = '{"status":"0000","data":{"opening_price":"6998000","closing_price":"7270000","min_price":"6750000","max_price":"7997000","average_price":"7188302.5804","units_traded":"78484.9241002","volume_1day":"78484.9241002","volume_7day":"335611.84181738","buy_price":"7268000","sell_price":"7274000","date":"1510563513031"}}';
$results = json_decode($api_results);

print_r($results);
var_dump($results->status);
$var = $results->data;
var_dump($var->opening_price);
?>

stdClass Object
(
    [status] => 0000
    [data] => stdClass Object
        (
            [opening_price] => 6998000
            [closing_price] => 7270000
            [min_price] => 6750000
            [max_price] => 7997000
            [average_price] => 7188302.5804
            [units_traded] => 78484.9241002
            [volume_1day] => 78484.9241002
            [volume_7day] => 335611.84181738
            [buy_price] => 7268000
            [sell_price] => 7274000
            [date] => 1510563513031
        )

)

string(4) "0000"
string(7) "6998000"

答案 5 :(得分:-2)

您已将json_decode()的第二个参数设置为true。这意味着json将转换为数组,因此您无法使用指针->访问数据(因为它不是对象)。

您可以访问以下数据:

var_dump($results['status'][0]);
var_dump($results['data'][0]['opening_price']);

P.S:尝试var_dump($results)查看确切数据,以便了解如何访问每个属性。