473,382 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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
Sep 5 '08 #1
5 1591
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
Sep 5 '08 #2
On Sep 5, 2:18 pm, Gregor Kofler <use...@gregorkofler.atwrote:
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
Sep 5 '08 #3
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
Sep 5 '08 #4
On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.atwrote:
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
Sep 5 '08 #5
Nick S meinte:
On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.atwrote:
>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
Sep 5 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
2
by: Kevin Newman | last post by:
Hello, I noticed that the JavaScript library for JSON posted on json.org (http://www.json.org/json.js) is modifying Object.prototype (adding a method - toJSONString). I thought this was...
23
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The current proposal, String.prototype.parseJSON, returns...
9
by: Jon Paal [MSMD] | last post by:
using json like ( {"Records": , "RecordCount":"1" } ) and jquery like: $.ajax({ .... success: function(json, status) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.