2014-09-24 93 views
0
提取JSON數據

的json數據如下:jsonpath-使用jsonpath

"query": { 
    "count": 1, 
    "url": [ 
    { 
    "execution-start-time": "1" 
    }, 
    { 
    "execution-time": "745" 
    } 
    ], 
    "service-time": "1481" 
    }, 
     results": { 
     "div": { 
     "class": "makers", 
     "ul": { 
     "li": [ 
      { 
      "a": { 
      "href": "nokia_x2_dual_sim-6383.php", 
      "img": { 
      "src": "Nokia-X2-Dual-SIM.jpg" 
      }, 
      "strong": { 
      "br": null, 
      "content": "Nokia\nX2 Dual SIM" 
      } 
      } 
      }, 
      { 
      "a": { 
      "href": "nokia_xl-6148.php", 
      "img": { 
      "src": "nokia-xl.jpg", 
      }, 
      "strong": { 
      "br": null, 
      "content": "Nokia\nXL" 
      } 
      } 
      } 
     ] 

現在我想提取"content"部分使用jsonpath.I這個JSON數據正在使用jsonpath,0.8.0.js爲這個目的。

我曾嘗試通過以下方式來解析JSON數據:

function ParseData(data) { 
    var result = jsonPath(data, "$.query.results[*].ul.li[*].strong.content"); 
    $('#carousel').empty(); 
    var html = ""; 
    for (i = 0; i < result.length; i++) { 
    // do something 
    } 

,我期待得到Nokia\nX2 Dual SIMNokia\nXL作爲輸出,但我的這個代碼不返回anything.I無法理解的問題。

如何從此json提取content數據?請幫忙。

回答

2

朋友們,我終於得到了我的問題的答案。我用以下代碼從上面給出的json數據中提取content

var result = jsonPath(data, "$.query.results[*].ul.li[*].a.strong.content"); 

現在我得到預期的輸出數據。