Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 5th, 2008, 01:25 PM
Nick S
Guest
 
Posts: n/a
Default 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
  #2  
Old September 5th, 2008, 01:35 PM
Gregor Kofler
Guest
 
Posts: n/a
Default 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
  #3  
Old September 5th, 2008, 01:45 PM
Nick S
Guest
 
Posts: n/a
Default 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
  #4  
Old September 5th, 2008, 02:25 PM
Gregor Kofler
Guest
 
Posts: n/a
Default 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
  #5  
Old September 5th, 2008, 02:55 PM
Nick S
Guest
 
Posts: n/a
Default 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
  #6  
Old September 5th, 2008, 03:15 PM
Gregor Kofler
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles