Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » JavaScript

Iteration through a json object

 
Jump to:  
 
Nick S
PostPosted: Fri Sep 05, 2008 12:20 pm    Post subject: Iteration through a json object
       
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
 

 
Gregor Kofler
PostPosted: Fri Sep 05, 2008 12:20 pm    Post subject: Re: Iteration through a json object
       
Nick S meinte:
Quote:
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"?

Quote:
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


--
LINK ::: Landschafts- und Reisefotografie
LINK ::: meine JS-Spielwiese
LINK ::: Bildagentur für den alpinen Raum
 

 
Gregor Kofler
PostPosted: Fri Sep 05, 2008 12:20 pm    Post subject: Re: Iteration through a json object
       
Nick S meinte:


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

Sigh. Please don't quote sigs...

Quote:
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) Smile

Lets see:

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

Please answer: What is the value of p?

Gregor


--
LINK ::: Landschafts- und Reisefotografie
LINK ::: meine JS-Spielwiese
LINK ::: Bildagentur für den alpinen Raum
 

 
Gregor Kofler
PostPosted: Fri Sep 05, 2008 12:20 pm    Post subject: Re: Iteration through a json object
       
Nick S meinte:
Quote:
On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.at> 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



--
LINK ::: Landschafts- und Reisefotografie
LINK ::: meine JS-Spielwiese
LINK ::: Bildagentur für den alpinen Raum
 

 
Nick S
PostPosted: Fri Sep 05, 2008 12:43 pm    Post subject: Re: Iteration through a json object
       
On Sep 5, 2:18 pm, Gregor Kofler <use...@gregorkofler.at> wrote:
Quote:
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 LINK ::: 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
PostPosted: Fri Sep 05, 2008 1:48 pm    Post subject: Re: Iteration through a json object
       
On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.at> wrote:
Quote:
Nick S meinte:

--http://photo.gregorkofler.at:::Landschafts- und LINK 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.

Quote:

Gregor

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

 
Nick S
PostPosted: Fri Sep 05, 2008 2:18 pm    Post subject: Re: Iteration through a json object
       
On Sep 5, 4:02 pm, Gregor Kofler <use...@gregorkofler.at> wrote:
Quote:
Nick S meinte:

On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.at> 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 LINK ::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum

he he ok ok, I've been at this for hours (not the problem, the app)
and I'm a little bog eyed Smile Thanks for the help. Even if it had a
dose of sarcasm in it Smile
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

ubezpieczenie ac Pozycjonowanie House painting estimate Quotations rejestracja samochodu