2012-02-24 69 views
2

所以我有一个JSON文件,这基本上是编码成JSON与PHP的Atom XML,每个条目看起来是这样的:从使用jQuery JSON文件获取@属性

{ 
    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()); 

感谢

回答

4

可以使用一键访问属性:

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

另外一个注意:正确的JSON必须具有双键名引号(请注意,JSON作为数据交换格式与定义脚本中的对象是有区别的)。如果您使用JSON语法在脚本中定义对象,则需要引用具有非法字符(例如@)的键名称。

这是一个小提琴,展示了一个工作示例。 http://jsfiddle.net/4YhTk/3/

+0

太棒了!谢谢,我会接受你的答案,一旦我可以 – QuirijnGB 2012-02-24 15:30:04

+0

是报价是在原始文件,他们消失了,因为铬扩展时复制/粘贴 – QuirijnGB 2012-02-24 15:48:39

+0

没问题,只要确保:) – HackedByChinese 2012-02-24 15:53:13