未捕获的错误:无法将stdClass类型的对象用作数组

时间:2019-03-21 19:29:56

标签: php json

我正在尝试从此JSON文件读取多个数据,但我不断收到各种错误,例如; “未捕获的错误:无法将stdClass类型的对象用作数组输入”或“试图获取非对象输入的属性'title'”。我在哪里做错了?

{{1}}

2 个答案:

答案 0 :(得分:0)

use Encode qw( decode_utf8 );

my $hex   = "48656c6c6f20576f726c64";
my $bytes = pack('H*', $hex);
my $text  = decode_utf8($bytes);

答案 1 :(得分:0)

您尝试获取数组的索引0。但是,变量不是数组,而是对象。解码时,必须到达带有标头的元素。

例如,如果要获取datePosted,则结构为

echo $baz->datePosted

如果您要获取addressRegion,则只需

echo $baz->jobLocation->address->addressRegion

print_r($ baz);

的结果
stdClass Object
(
    [@context] => http://schema.org
    [@type] => JobPosting
    [title] => UX Designer
    [url] => https://www.google.com
    [datePosted] => 2019-03-15T16:38+00:00
    [validThrough] => 2019-03-22T16:38+00:00
    [description] => Description
    [industry] => IT
    [employmentType] => Permanent
    [hiringOrganization] => stdClass Object
        (
            [@type] => Organization
            [name] => PCR
            [url] => https://www.google.com
            [logo] => https://www.google.com
        )

    [jobLocation] => stdClass Object
        (
            [@type] => Place
            [address] => stdClass Object
                (
                    [@type] => PostalAddress
                    [addressLocality] => Acton Green
                    [addressRegion] => London
                    [postalCode] => W4 5YB
                    [addressCountry] => GB
                )

            [geo] => stdClass Object
                (
                    [@type] => GeoCoordinates
                    [latitude] => 51.4937
                    [longitude] => -0.275693
                )

        )

)