By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,661 Members | 1,917 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,661 IT Pros & Developers. It's quick & easy.

Accessing JSON Property

P: 7
I have the following JSON obj/code:

Expand|Select|Wrap|Line Numbers
  1. var json_logs = 
  2. {
  3. "countries": [ "US","AU"],
  4. "US":{"num_calls":"555","time":"432"},
  5. "AU":{"num_calls":"212","time":"233"}
  6. }
  7.  
  8. var call = json_logs.evalJSON();
  9.  
  10. for(i=0;i< call.countries.length; i++){
  11.  alert(call[countries[i]].num_calls);
  12. }
However it doesn't return/prompt anything. I've tried a few different variations and just can't for the life of me remember how/if I can access an object property through this sort of method.

What am I missing?
Jan 24 '08 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,327
hi ...

since i don't know what your eval does i removed it since json_logs is an javascript object already. the following should do the trick then:

Expand|Select|Wrap|Line Numbers
  1. var json_logs = {
  2.     "countries": [ "US","AU"],
  3.     "US": {"num_calls":"555","time":"432"},
  4.     "AU": {"num_calls":"212","time":"233"}
  5. };
  6.  
  7. for(var i = 0; i < json_logs.countries.length; i++){
  8.     alert(json_logs[json_logs.countries[i]].num_calls);
  9. }
  10.  
kind regards
Jan 24 '08 #2

P: 7
Cheers :D
Works a treat, it came to me in the shower, can't believe it escaped me.

It was a smaller part of a large ajax/json thing.

Thanks again!
Jan 24 '08 #3

gits
Expert Mod 5K+
P: 5,327
no problem ... glad to hear you got it working ... post back to the forums anytime you have more questions :)

kind regards
Jan 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.