我的 JSON 文件中的那些 @attributes
内容是什么?我如何使用 JQuery 读取它?
我使用的 JSON“文本”是通过 PHP 中的 json_encode
从自定义对象数组生成的。
这是一个简化的 JSON 文件:
{ "movies" : [ { "url":"http:\/\/www.youtube.com\/watch?v=Nsd7ZcXnL6k", title":{"@attributes":{"type":"text"},"0":"**Title here**"} ] }
我可以使用以下代码轻松读取 URL:
$.getJSON(url, function(json){
$.each(json.movies,function(i,item) {
alert(item.url);
});
});
如何读取标题此处的标题
值?
更新
好吧,我仍然不知道 @attributes
是什么,但我知道为什么它们会出现在我的最终 JSON 文件中。我使用 $sxml = simplexml_load_file($feedURL);
读取 XML,然后使用 $sxml->title
读取标题,这显然不是一个字符串,而是一些一种 PHP 对象。
而不是
$this->title = $sxml->title
我用过
$this->title = $sxml->title . ""
(Ot 将对象转换为字符串值)。也许有更智能的方法来做到这一点?
如果您有最新的 PHP,它支持强制转换,因此您可以使用
(string)$xml->title
它会起作用的。
最佳答案
$.getJSON(url, function(json){
$.each(json.movies,function(i,item) {
alert(item.url);
alert(item.title[0]);
});
});
关于php - 读取由 PHP 生成的包含 @attributes 的 JSON(使用 jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2152031/