2012-05-30 44 views
1

我想解析一个JSON文件使用PHP和json_decode,但是我有difficulting这样做时返回的JSON名称空间。例如:PHP json_decode - 处理命名空间

$json_ouput = json_decode($json); 

foreach ($json_ouput->feed as $feed) { 

    /* 
    Here is the problem, $feed contains a namespaced key 
    $feed->ab:test->value // Does not work :(
    */ 
} 

请告诉我这里最好的解决方案?

+2

'%json_output'?在那里做'%'是什么?你能向我们展示一些你正在使用的JSON吗? – Sampson

+0

用$替换%,然后重试! –

+0

这看起来不像PHP。 –

回答

4

和往常一样。

$feed->{'ab:test'}->value 
0

在你删除一个变量,周围的字符串中的字符之间的模糊以同样的方式很多,你可以使用{}来拼凑一个访问的一部分:

$json = '{"feed":[{"ab:test":{"value":"foo"}},{"ab:test":{"value":"bar"}}]}'; 
$json_output = json_decode($json); 

foreach ($json_output->feed as $feed) { 

    // Outputs: foo bar 
    print_r($feed->{'ab:test'}->value); 

} 

演示:http://codepad.org/MYYwOJj2