Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Question possible to loop through a JSON object?

    i have a JSON object (w/1 level - (key value pairs)) returned to the client that needs to be used to fill in a form. Is there a way to use a FOR loop to set every value in the key fields?

    i have looked for a few hours and it seems like there is no easy way to do it., i cant access the key names except by name or by index, and both of those give me the value, not the key name, and i need both.

    thanks,
    Leon

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by Leeoniya View Post
    i have a JSON object (w/1 level - (key value pairs)) returned to the client that needs to be used to fill in a form. Is there a way to use a FOR loop to set every value in the key fields?
    Code:
    var
        key;
    for(key in JSONObject)
        if(JSONObject.hasOwnProperty(key)){
            /* do something with JSONObject[key] */
        }
    Beware that Object.prototype.hasOwnProperty does not work in Safari 1.3 (last version that can be used on Mac OS X 10.3 (Panther)). I don't know whether it works in later versions.


    If you want a version that is safe for Safari 1.3:
    Code:
    var
        key;
    for(key in JSONObject)
        if(!(key in Object.prototype) || JSONObject[key]!==Object.prototype[key]){
            /* do something with JSONObject[key] */
        }


    WARNING: I haven't actually tried out this code, so it might be buggy.
    Last edited by liorean; 08-28-2007 at 08:38 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    liorean, please, why is necessary the Object.prototype? Couldn't a simple loop solve the problem? Or I miss something? I am also interested in modifying a JSON object's key/value in a short manner.... I use to think that prototype and JSON have a not so closed relationship
    Last edited by Kor; 08-28-2007 at 09:58 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by Kor View Post
    liorean, please, why is necessary the Object.prototype? Couldn't a simple loop solve the problem? Or I miss something? I am also interested in modifying a JSON object's key/value in a short manner.... I use to think that prototype and JSON have a not so closed relationship
    Because the for(key in obj) syntax will not only walk through all properties on the object, it will also walk through the full prototype chain. So any property on Object.prototype will appear in the loop. Because of that you have to filter out the properties that come from the prototype chain.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • Users who have thanked liorean for this post:

    Kor (08-28-2007)

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    OK, thanks, I'll think about...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •