2012-10-12 27 views
0

檢索字符串數組鍵值對,我不能夠在下面的輸出JSON作爲「鑰匙」,「值」對來從「statisticalEvidence」的值。如何從JSON jQuery中

JSON輸出:

{ 
    "query": { 
     "count": 1, 
     "lang": "en-US", 
     "results": { 
      "entities": { 
       "total": "10", 
       "lang": "en-US", 
       "entity": [ 
        { 
        "id": "FZZNDK3XM3VIPLW76QHCAYF2D4", 
        "rank": "10", 
        "scores": { 
         "score": { 
          "attribute": "contentCreator", 
          "context": "yct:001000012", 
          "value": "1.0", 
          "statisticalEvidence": "\"{\"totalNoOfAbusiveComments\":0,\"totalNoOfThumbsDown\":22,\"totalNoOfPositiveRatedComments\":70,\"totalNoOfReplies\":8,\"totalNoOfLikedComments\":20,\"totalNoOfEngagingReplies\":13,\"totalNoOfThumbsUp\":35,\"totalNoOfComments\":79}\"" 
         } 
        }} 
       ] 
      } 
     } 
    } 
} 

我能得到一個 「ID」,如下:

$.get(url,function(data) { 
    var json = jQuery.parseJSON(data); 
    for(var i in json.query.results.entities.entity){ 
    var id = json.query.results.entities.entity[i].id; 
     } 
}); 

請建議如何在statisticalEvidence值作爲數組提取。

+0

你想要這樣的東西? '[{key:'count',value:1},{key:'lang',value:'en-US'}]'? – Brad

+0

@Brad ..其實我想獲取「totalNoOfAbusiveComments」:0,「totalNoOfThumbsDown」:22 – Ravinandan

+0

這不是一個數組,這是一個對象。 JavaScript中的數組只能有數字索引。 – Brad

回答

0

嘗試以下,

//should return the string 
var se = json.query.results.entities.entity[i].scores.score.statisticalEvidence 

然後使用$.parseJSON

se = $.parseJSON(se); 

全碼:

$.get(url,function(data) { 
    var json = jQuery.parseJSON(data); 
    for(var i in json.query.results.entities.entity){ 
     var id = json.query.results.entities.entity[i].id; 
     var se = $.parseJSON(json.query.results.entities.entity[i].scores.score.statisticalEvidence); 
    } 
}); 

注:statisticalEvidence在你的數據是一個對象不是一個數組。

+0

@vega ..感謝您的快速響應。其實我試過這個,我遇到了下面的錯誤。語法錯誤:JSON.parse:後JSON數據 源文件 – Ravinandan

+0

@Ravinandan嘗試添加這些行..'SE = se.replace(/ \\/g的, 「」)'然後'$ .parseJSON意想不到非空白字符( se.substring(1,se.length - 1))'應該返回一個對象 –

+0

@Vega ..非常感謝... $。parseJSON(se.substring(1,se.length - 1))做了這個把戲... – Ravinandan