Accessing JSON Property

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bitslong
    New Member
    • Apr 2007
    • 7

    Accessing JSON Property

    I have the following JSON obj/code:

    Code:
    var json_logs = 
    {
    "countries": [ "US","AU"],
    "US":{"num_calls":"555","time":"432"},
    "AU":{"num_calls":"212","time":"233"}
    }
    
    var call = json_logs.evalJSON();
    
    for(i=0;i< call.countries.length; i++){
     alert(call[countries[i]].num_calls);
    }
    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?
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    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:

    [CODE=javascript]
    var json_logs = {
    "countries" : [ "US","AU"],
    "US": {"num_calls":"5 55","time":"432 "},
    "AU": {"num_calls":"2 12","time":"233 "}
    };

    for(var i = 0; i < json_logs.count ries.length; i++){
    alert(json_logs[json_logs.count ries[i]].num_calls);
    }
    [/CODE]
    kind regards

    Comment

    • Bitslong
      New Member
      • Apr 2007
      • 7

      #3
      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!

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        no problem ... glad to hear you got it working ... post back to the forums anytime you have more questions :)

        kind regards

        Comment

        Working...