2014-06-06 28 views
0

我有一個JSON從serverlet返回如下:HOWTO環通JSON在JavaScript/jQuery和讀取數據正確

{ 
"0": { 
    "id": "1252380", 
    "text": "This whole #BundyRanch thing stinks to high hell. Can it be a coincidence that Harry Reid n his son have a financial interest in this land?", 
    "date": "2014-05-31", 
    "name": "Code Green Hackathon", 
    "url": "http://pbs.twimg.com/profile_images/424023521040166913/1dAg1g_r_normal.jpeg", 
    "username": "t.getScreenName()" 
}, 
"1": { 
    "id": "1252378", 
    "text": "Harry Reid threatens private property in America http://t.co/uBX7lau0EP via @worldnetdaily", 
    "date": "2014-05-31", 
    "name": "Murray Leismeister", 
    "url": "http://pbs.twimg.com/profile_images/378800000559954056/724415b96df3a22d2a9cc6e2b90052dc_normal.jpeg", 
    "username": "t.getScreenName()" 
} 

}

,並在JavaScript我這樣做是爲了看我的JSON:

 for(var item in jsresult) 
      { 
       alert(item); 
      } 

我期望看到兩個警報顯示0,然後1,但我沒有。相反,我得到了六個警示分別顯示0,1 0,1 0,1,我檢查了服務器,並且我確信唯一的賈森回報是我在上面發佈的回報。所以我的JavaScript代碼有些問題。任何人都可以幫助(我很困惑)

更新:

這裏是我的Ajax調用

$.get(getServletURLTweet(), { 
      timeRange : tRange, 
      entities : enIDs, 
      topics:tpIDs 
     }, function(results) { 
      jsresult = jQuery.parseJSON(results); 
      for(var item in jsresult) 
      { 
       alert(item); 
      }}); 
+0

你也許調用此代碼3倍? – Halcyon

+0

console.log(jsresult)的輸出是什麼; – xxcezz

+0

你使用jQuerys'$ .each()'得到同樣的錯誤嗎? – Nano

回答

1

你可以試試這個

var data = [ 
{"Id": 10, "PageName": "home"}, 
{"Id": 11, "PageName": "about"}, 
{"Id": 12, "PageName": "more"} 
]; 

$.each(data, function(i, item) { 
alert(data[i].PageName); 
});​