【问题标题】:Reading JSON (with jQuery) produced by PHP containing @attributes读取由 PHP 生成的包含 @attributes 的 JSON(使用 jQuery)
【发布时间】:2010-01-28 03:08:56
【问题描述】:

我的 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);  
    });
 });

标题Title here值怎么读?

更新

好吧,我仍然不知道@attributes 是什么,但我知道它们为什么会出现在我的最终 JSON 文件中。我使用 $sxml = simplexml_load_file($feedURL); 读取 XML,然后使用 $sxml->title 读取标题,这显然不是字符串,而是某种 PHP 对象。

代替

$this->title = $sxml->title

我用过

$this->title = $sxml->title . ""

(Ot 将对象转换为字符串值)。也许有更智能的方法来做到这一点?

如果你有最近的PHP,它支持强制转换,所以你可以使用

(string)$xml->title

它会起作用的。

【问题讨论】:

    标签: php jquery attributes json


    【解决方案1】:
    $.getJSON(url, function(json){
    
        $.each(json.movies,function(i,item) {
            alert(item.url); 
            alert(item.title[0]); 
        });
     });
    

    【讨论】:

    • 啊,这就是 0 的用途!谢谢! ;)
    猜你喜欢
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多