2012-02-24 31 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