使用PHP将JSON解码为数组或对象的问题

时间:2010-10-12 01:23:12

标签: php arrays json object

我正在尝试使用PHP来处理JSON,并且无法将其解析为对象或字符串。这是JSON提要:

stdClass Object ( [Response] => stdClass Object ( [Meta] => stdClass Object ( [ExecutionTime] => 0.41602396965 ) [Data] => stdClass Object ( [Entity] => stdClass Object ( [id] => 1 [name] => Wal-Mart Stores [description] => Retail merchandising [summary] => [start_date] => [end_date] => [is_current] => 1 [primary_type] => Org [parent_id] => [updated_at] => 2010-09-27 11:11:55 [uri] => http://littlesis.org/org/1/Wal-Mart_Stores [api_uri] => http://api.littlesis.org/entity/1.xml [types] => Org,Business,PublicCompany [name_nick] => [employees] => [revenue] => 378799000000 [fedspending_id] => 336092 [lda_registrant_id] => 40305 [annual_profit] => [ticker] => WMT [sec_cik] => 104169 [Aliases] => stdClass Object ( [Alias] => Array ( [0] => Wal Mart [1] => Walmart ) ) ) ) ) ) 

显然,我也可以将其作为数组访问。无论哪种方式,我都无法解析它。我试过,例如:

$json->Response->Data->Entity->id;

或数组:

$json['Response']['Data']['Entity']['id'];

这些都不起作用。对于对象,我被告知“试图获取非对象的属性”,并且对于字符串没有任何显示。

我更习惯于在Python中处理JSON,而json ['Response'] ['Data'] ['Entity'] ['id']在Python中运行得很好,但我需要在PHP中执行此操作。我确信我做的事情非常明显,但无法解决它。

1 个答案:

答案 0 :(得分:1)

首先在JSON字符串上使用json_decode(),然后php可以将其解析为数组。