I'm trying to use PHP to process JSON and am having trouble parsing it as either an object or a string. Here is the JSON feed:
我正在尝试使用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 ) ) ) ) ) )
Obviously, I can also access it as an array. Either way, however, I can't actually parse it. I have tried, for example:
显然,我也可以将其作为数组访问。但无论如何,我无法解析它。我试过,例如:
$json->Response->Data->Entity->id;
or for arrays:
或者对于数组:
$json['Response']['Data']['Entity']['id'];
Neither of these works. For objects, I'm told "Trying to get property of non-object" and for strings nothing displays.
这些都不奏效。对于对象,我被告知“试图获取非对象的属性”,并且对于字符串没有任何显示。
I'm more used to dealing with JSON in Python, and json['Response']['Data']['Entity']['id'] works just fine in Python but I need to do this in PHP. I'm sure I'm doing something very obvious, but can't puzzle through it.
我更习惯于在Python中处理JSON,而json ['Response'] ['Data'] ['Entity'] ['id']在Python中运行得很好但我需要在PHP中执行此操作。我确信我做的事情非常明显,但无法解决这个问题。
1 个解决方案
#1
1
Use json_decode()
on the JSON string first and then php can parse it as an array.
首先在JSON字符串上使用json_decode(),然后php可以将其解析为数组。
#1
1
Use json_decode()
on the JSON string first and then php can parse it as an array.
首先在JSON字符串上使用json_decode(),然后php可以将其解析为数组。