php - 读取由 PHP 生成的包含 @attributes 的 JSON(使用 jQuery)

标签 php jquery attributes json

我的 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/

相关文章:

php - Icalendar 事件 RSVP 是否有标准和实现

php - 在 WooCommerce 管理订单仪表板中更新客户密码

jQuery :contains with html

jquery - 从 jQuery 1.5 升级到 1.10.2,代码无法正常工作

c++ - C++ 中的 cast 属性

css - 如何通过 TagHelper 获取 HTML 属性的值?

c# - 如何访问属性内部的属性?

php - 如何使用mysql根据其中的整数对varchar id进行排序

php - jQuery,更新正确的字段

javascript - html 选择启用禁用属性