Iteration through a json object

Discussion in 'Javascript' started by Gregor Kofler, Sep 5, 2008.

  1. Nick S meinte:
    > Hi
    >
    > Is there anyway to iterate through a json object and retrieve the name
    > of the property as well as the value.


    What do you mean by "json object"?

    > For exampe. Imagine this object
    >
    > {
    > title: '1',
    > bedrooms: '2',
    > bathrooms: '3',
    > description: '4',
    > country: '5',
    > location: '6',
    > price: '7',
    > }
    >
    > I want to loop through it and have a variable that holds title, then
    > bedrooms then bathrooms.... as well as one that holds 1 then 2 then
    > 3... respectively.
    >
    > Is that possible?


    You are looking for "for ... in". Mind the possible problems with
    (augmented) prototypes, though.

    Gregor


    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
    Gregor Kofler, Sep 5, 2008
    #1
    1. Advertising

  2. Gregor Kofler

    Nick S Guest

    Hi

    Is there anyway to iterate through a json object and retrieve the name
    of the property as well as the value.

    For exampe. Imagine this object

    {
    title: '1',
    bedrooms: '2',
    bathrooms: '3',
    description: '4',
    country: '5',
    location: '6',
    price: '7',
    }

    I want to loop through it and have a variable that holds title, then
    bedrooms then bathrooms.... as well as one that holds 1 then 2 then
    3... respectively.

    Is that possible?

    Thanks
    Nick S, Sep 5, 2008
    #2
    1. Advertising

  3. Gregor Kofler

    Nick S Guest

    On Sep 5, 2:18 pm, Gregor Kofler <> wrote:
    > Nick S meinte:
    >
    > > Hi

    >
    > > Is there anyway to iterate through a json object and retrieve the name
    > > of the property as well as the value.

    >
    > What do you mean by "json object"?
    >
    >
    >
    > > For exampe. Imagine this object

    >
    > > {
    > > title: '1',
    > > bedrooms: '2',
    > > bathrooms: '3',
    > > description: '4',
    > > country: '5',
    > > location: '6',
    > > price: '7',
    > > }

    >
    > > I want to loop through it and have a variable that holds title, then
    > > bedrooms then bathrooms.... as well as one that holds 1 then 2 then
    > > 3... respectively.

    >
    > > Is that possible?

    >
    > You are looking for "for ... in". Mind the possible problems with
    > (augmented) prototypes, though.
    >
    > Gregor
    >
    > --http://photo.gregorkofler.at::: Landschafts- und Reisefotografiehttp://web.gregorkofler.com ::: meine JS-Spielwiesehttp://www.image2d.com :::Bildagentur für den alpinen Raum


    err, ok, just forget my crappy grasp of the terminology for a second.
    I've outlined the problem pretty clearly. I do use "for...in" but I
    can't get to the name of each item. (And if that's not clear I mean
    the thing on the left of the colon) :)

    Thanks
    Nick S, Sep 5, 2008
    #3
  4. Nick S meinte:


    >> --http://photo.gregorkofler.at::: Landschafts- und Reisefotografiehttp://web.gregorkofler.com ::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum


    Sigh. Please don't quote sigs...

    > err, ok, just forget my crappy grasp of the terminology for a second.
    > I've outlined the problem pretty clearly. I do use "for...in" but I
    > can't get to the name of each item. (And if that's not clear I mean
    > the thing on the left of the colon) :)


    Lets see:

    var obj = { ... }, p;
    for(p in obj) {
    window.alert(obj[p]);
    };

    Please answer: What is the value of p?

    Gregor


    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
    Gregor Kofler, Sep 5, 2008
    #4
  5. Gregor Kofler

    Nick S Guest

    On Sep 5, 3:10 pm, Gregor Kofler <> wrote:
    > Nick S meinte:
    >
    > >> --http://photo.gregorkofler.at:::Landschafts- und Reisefotografiehttp://web.gregorkofler.com::: meine JS-Spielwiesehttp://www.image2d.com :::Bildagentur für den alpinen Raum

    >
    > Sigh. Please don't quote sigs...
    >
    > > err, ok, just forget my crappy grasp of the terminology for a second.
    > > I've outlined the problem pretty clearly. I do use "for...in" but I
    > > can't get to the name of each item. (And if that's not clear I mean
    > > the thing on the left of the colon) :)

    >
    > Lets see:
    >
    > var obj = { ... }, p;
    > for(p in obj) {
    > window.alert(obj[p]);
    >
    > };
    >
    > Please answer: What is the value of p?


    Thanks. yes, this is what I do, however, I don't just want the value,
    I also want the name of the property.

    >
    > Gregor
    >
    > --http://photo.gregorkofler.at::: Landschafts- und Reisefotografiehttp://web.gregorkofler.com ::: meine JS-Spielwiesehttp://www.image2d.com :::Bildagentur für den alpinen Raum
    Nick S, Sep 5, 2008
    #5
  6. Nick S meinte:
    > On Sep 5, 3:10 pm, Gregor Kofler <> wrote:


    >> var obj = { ... }, p;
    >> for(p in obj) {
    >> window.alert(obj[p]);
    >>
    >> };
    >>
    >> Please answer: What is the value of p?


    >
    > Thanks. yes, this is what I do, however, I don't just want the value,
    > I also want the name of the property.


    Jeez! The value of p *is* the name of the property. obj[p] gives you the
    value of the property p.

    Gregor



    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
    Gregor Kofler, Sep 5, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rudi
    Replies:
    5
    Views:
    5,008
  2. Florian Frank
    Replies:
    0
    Views:
    238
    Florian Frank
    Jun 30, 2009
  3. Aaron
    Replies:
    2
    Views:
    519
    dhtml
    Apr 10, 2011
  4. sajuptpm
    Replies:
    2
    Views:
    331
    sajuptpm
    Dec 28, 2012
  5. Acácio Centeno
    Replies:
    1
    Views:
    249
    dieter
    Feb 15, 2013
Loading...

Share This Page