Connecting Tech Pros Worldwide Forums | Help | Site Map

Iteration through a json object

Nick S
Guest
 
Posts: n/a
#1: Sep 5 '08
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
Guest
 
Posts: n/a
#2: Sep 5 '08

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


--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Nick S
Guest
 
Posts: n/a
#3: Sep 5 '08

re: Iteration through a json object


On Sep 5, 2:18 pm, Gregor Kofler <use...@gregorkofler.atwrote:
Quote:
Nick S meinte:
>
Quote:
Hi
>
Quote:
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
>
Quote:
{
title: '1',
bedrooms: '2',
bathrooms: '3',
description: '4',
country: '5',
location: '6',
price: '7',
}
>
Quote:
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.
>
Quote:
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
Gregor Kofler
Guest
 
Posts: n/a
#4: Sep 5 '08

re: Iteration through a json object


Nick S meinte:

Quote:
Quote:
>--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...
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) :)
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
Nick S
Guest
 
Posts: n/a
#5: Sep 5 '08

re: Iteration through a json object


On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.atwrote:
Quote:
Nick S meinte:
>
Quote:
Quote:
--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...
>
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) :)
>
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 Reisefotografiehttp://web.gregorkofler.com ::: meine JS-Spielwiesehttp://www.image2d.com :::Bildagentur für den alpinen Raum
Gregor Kofler
Guest
 
Posts: n/a
#6: Sep 5 '08

re: Iteration through a json object


Nick S meinte:
Quote:
On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.atwrote:
Quote:
Quote:
>var obj = { ... }, p;
>for(p in obj) {
> window.alert(obj[p]);
>>
>};
>>
>Please answer: What is the value of p?
Quote:
>
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
Closed Thread


Similar JavaScript / Ajax / DHTML bytes