У меня есть json, возвращаемый с сервера, как показано ниже:
{
"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);
}});
Вы можете попробовать это
var data = [
{"Id": 10, "PageName": "home"},
{"Id": 11, "PageName": "about"},
{"Id": 12, "PageName": "more"}
];
$.each(data, function(i, item) {
alert(data[i].PageName);
});