php - 使用 jQuery 从 JSON 文件获取 @attributes

标签 php jquery json attributes

所以我有一个 JSON 文件,它基本上是使用 PHP 编码为 JSON 的 ATOM XML,每个条目如下所示:

{
    id: "info:fedora/demo:SmileyBucket/RELS-EXT/2008-07-02T05:09:42.937Z",
    title: "RELS-EXT.0",
    updated: "2008-07-02T05:09:42.937Z",
    category: [
        {
            @attributes: {
                term: "info:fedora/fedora-system:FedoraRELSExt-1.0",
                scheme: "info:fedora/fedora-system:def/model#formatURI"
            }
        },
        {
            @attributes: {
                term: "RDF Statements about this object",
                scheme: "info:fedora/fedora-system:def/model#label"
            }
        },
        {
            @attributes: {
                term: "500",
                scheme: "info:fedora/fedora-system:def/model#length"
            }
        }
    ],
    content: {
        @attributes: {
            type: "application/rdf+xml"
        }
    }
}, 

我可以获得除@attributes 中的内容之外的所有数据。 这是我的 jQuery 的片段:

$("#content-pane").text("ID: " +json.id);
$("#content-pane").append("<br/> Title: " +json.title);
$("#content-pane").append("<br/> Attributes: " +json.entry[5].content.toString());

谢谢

最佳答案

您可以使用 key 访问属性:

var attributes = json.category[0]["@attributes"];

另一个注意事项:正确的 JSON 必须将键名称放在双引号中(请注意,作为数据交换格式的 JSON 与在脚本中定义对象之间存在差异)。如果您使用 JSON 语法在脚本中定义对象,则需要引用包含非法字符(例如 @)的键名。

这是一个显示工作示例的 fiddle 。 http://jsfiddle.net/4YhTk/3/

关于php - 使用 jQuery 从 JSON 文件获取 @attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9433242/

相关文章:

javascript - 当我点击一张图片时,它会弹出,但我想模糊其余部分

javascript - 当对象非常多并且我试图在范围内访问它时,为什么我会得到 "Can not read property 0 of undefined"?

php - ARC2(PHP 语义网库)错误地将 UTF-8 文件双重转换为 UTF-8

javascript - 有没有更合适的方法来使用 AJAX 处理错误?

javascript - Jquery 隐藏功能有时不适用于幻灯片

javascript - 如何向 textfield.JS/Jquery 中具有特定文本值的 <img> 标签添加属性

java - AWS API Gateway POST 请求不适用于 Lambda 函数

c# - 使用 JSON.Net 自定义 JSON 到 XML 的转换

php - 如何在 html div 中显示 php 文件输出?

php - “node”不被识别为内部或外部命令、可操作程序或批处理文件。 - 浏览器截图