admin管理员组文章数量:1291047
In writing a django app, I am returning the following json on a jQuery ajax call:
{
"is_owner": "T",
"author": "me",
"overall": "the surfing lifestyle",
"score": "1",
"meanings": {
"0": "something",
"1": "something else",
"3": "yet something else",
"23": "something random"
},
"user vote": "1"
}
In the javascript/jQuery callback function, I can access the is_owner, author, etc. easily enough.
is_owner = json.is_owner;
author = json.author;
But for meanings, the numbers are different depending on what it pulls from the server. On the server side for the meanings part, right now what I'm doing is constructing a dictionary like so:
meanings_dict = {}
meanings = requested_tayke.meanings.all()
for meaning in meanings:
meanings_dict[meaning.location] = meaning.text
and then returning a json I create like this:
test_json = simplejson.dumps({'is_owner':is_owner, 'overall':overall, 'score':str(score),'user vote':str(user_vote), 'author': author, 'meanings' : meanings_dict })
print test_json
return HttpResponse(test_json)
My question is this: how do I access the 'meanings' data from my json in javascript? I need to loop through all of it. Maybe I need to be loading it into json differently. I have full control so of both the server and client side so I'm willing to change either to make it work. Also worth noting: I'm not using Django's serialize functionality. I couldn't make it work with my situation.
In writing a django app, I am returning the following json on a jQuery ajax call:
{
"is_owner": "T",
"author": "me",
"overall": "the surfing lifestyle",
"score": "1",
"meanings": {
"0": "something",
"1": "something else",
"3": "yet something else",
"23": "something random"
},
"user vote": "1"
}
In the javascript/jQuery callback function, I can access the is_owner, author, etc. easily enough.
is_owner = json.is_owner;
author = json.author;
But for meanings, the numbers are different depending on what it pulls from the server. On the server side for the meanings part, right now what I'm doing is constructing a dictionary like so:
meanings_dict = {}
meanings = requested_tayke.meanings.all()
for meaning in meanings:
meanings_dict[meaning.location] = meaning.text
and then returning a json I create like this:
test_json = simplejson.dumps({'is_owner':is_owner, 'overall':overall, 'score':str(score),'user vote':str(user_vote), 'author': author, 'meanings' : meanings_dict })
print test_json
return HttpResponse(test_json)
My question is this: how do I access the 'meanings' data from my json in javascript? I need to loop through all of it. Maybe I need to be loading it into json differently. I have full control so of both the server and client side so I'm willing to change either to make it work. Also worth noting: I'm not using Django's serialize functionality. I couldn't make it work with my situation.
Share Improve this question asked Mar 16, 2010 at 1:20 PhilPhil 1371 silver badge10 bronze badges1 Answer
Reset to default 11it works (roughly) like how a dict works in python: you can iterate over the keys in a json object.
var meanings = json.meanings;
for (var key in meanings )
var value = meanings[key];
it might break if you use a naughty library that adds elements to object's prototype, so for defensive purposes, the established good practice is to write
for(var key in meanings)
if (meanings.hasOwnProperty(key))
var value = meanings[key];
本文标签: jqueryLooping through pythondictionaryturnedintojson in javascriptStack Overflow
版权声明:本文标题:jquery - Looping through python-dictionary-turned-into-json in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741524439a2383382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论