访问属性名称中含有禁止字符的对象属性
我正在使用 PHP 的 YouTube 数据 API 并以 json 格式请求特定用户的视频源。 json_decode 后的结果如下(缩短的示例):
stdClass Object
(
[version] => 1.0
[encoding] => UTF-8
[feed] => stdClass Object
(
[xmlns] => http://www.w3.org/2005/Atom
[xmlns$media] => http://search.yahoo.com/mrss/
[xmlns$openSearch] => http://a9.com/-/spec/opensearchrss/1.0/
[xmlns$gd] => http://schemas.google.com/g/2005
[xmlns$yt] => http://gdata.youtube.com/schemas/2007
)
)
我的问题是:如何使用 PHP 访问节点“xmlns$media”?在美元符号内它不起作用,或者有什么我还没有得到的方法吗?
I'm working with the YouTube Data API per PHP and requesting a video feed from a specific user in json-format. The result after json_decode is the following (shortened example):
stdClass Object
(
[version] => 1.0
[encoding] => UTF-8
[feed] => stdClass Object
(
[xmlns] => http://www.w3.org/2005/Atom
[xmlns$media] => http://search.yahoo.com/mrss/
[xmlns$openSearch] => http://a9.com/-/spec/opensearchrss/1.0/
[xmlns$gd] => http://schemas.google.com/g/2005
[xmlns$yt] => http://gdata.youtube.com/schemas/2007
)
)
My question is: how can I access for example the node "xmlns$media" with PHP? Within the dollar-sign it won't work or is there a way which I didn't get yet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将起作用:
或者,您可以告诉 json_decode 返回一个数组:
This will work:
Alternatively, you can tell
json_decode
to return an array:如果您使用单引号而不是双引号,则不会计算美元符号。一般来说,您应该养成使用单引号的习惯,除非您确实需要双引号(例如转义的十六进制值)
If you use single-quotes instead of double-quotes, the dollar sign won't be evaluated. Generally speaking, you should get in the habit of using single-quotes unless you really need double-quotes for something (such as escaped hex values)