2014-03-05 90 views

回答

0

如果您能夠舉例說明一些JSON數據,那將會很棒。根據你寫的內容,我懷疑你正在尋找類似的東西(這裏是JSFilddle:http://jsfiddle.net/hbi99/4WYkc/);

var data = { 
     "store": { 
      "book": [ 
      { 
       "@price": 12.99, 
       "title": "Sword of Honour", 
       "category": "fiction", 
       "author": "Evelyn Waugh" 
      }, 
      { 
       "@price": 22.99, 
       "title": "The Lord of the Rings", 
       "category": "fiction", 
       "author": "J. R. R. Tolkien", 
       "isbn": "0-395-19395-8" 
      } 
      ], 
      "bicycle": { 
      "@price": 19.95, 
      "brand": "Cannondale", 
      "color": "red" 
      } 
     } 
    }, 
    found = JSON.search(data, '//*[contains(title, "Lord")]'); 

document.getElementById('output').innerHTML = found[0].title; 

JSONPath不是標準化的「查詢語言」,而是XPath。 JS庫DefiantJS使用「搜索」方法擴展了全局對象JSON,通過它可以使用XPath表達式查詢JSON結構。該方法將匹配返回爲類似數組的對象。

看到更多的例子,在這裏檢查出現場XPath計算器: http://defiantjs.com/#xpath_evaluator

+0

感謝您的。但是,您的答案會返回不是我想要的整個標題。我需要標題的一部分,例如標題中的「of」之後的任何內容。 – Leon

+0

我想你正在尋找一種解決方案,不會導致您的結尾後解析。如果你找到這樣的解決方案,在這裏分享。 –

相關問題