473,405 Members | 2,310 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,405 software developers and data experts.

problems with json in a for loop

I'm trying to retrieve some values from a json object, but instead it's
giving me the property name. For example:

var json = { "glossary": { "title": "example glossary" } };
console.log(json);
alert(json.glossary.title);
for (var x in json) { console.log(x); alert(x.title); }

This will show me the json object in the console with glossary and
title underneath it. When the alert for json.glossary.title fires, it
will show me "example glossary". however, inside of the for loop, I
see "glossary" in the console log (instead of the glossary object) and
undefined for the x.title alert.

What's going on? Could someone please explain why it's behaving this
way?

Dec 7 '06 #1
3 10150
VK

Adam wrote:
I'm trying to retrieve some values from a json object, but instead it's
giving me the property name. For example:

var json = { "glossary": { "title": "example glossary" } };
console.log(json);
alert(json.glossary.title);
for (var x in json) { console.log(x); alert(x.title); }

This will show me the json object in the console with glossary and
title underneath it. When the alert for json.glossary.title fires, it
will show me "example glossary". however, inside of the for loop, I
see "glossary" in the console log (instead of the glossary object) and
undefined for the x.title alert.

What's going on? Could someone please explain why it's behaving this
way?
That will become much clearer if you try alert(typeof x); which gives
you "string".

That is a common misconception about for-in loop that it returns
enumerable *properties* of the object. I saw it written even in some
overall good manuals.

for-in loops returns string *names* of enumerable properties in the
given object.

var json = { "glossary": { "title": "example glossary" } };
alert(json);
alert(json.glossary.title);
for (var x in json) { alert(x); alert(json[x].title); }

Dec 7 '06 #2
Thanks! That works great. I totally see what's happening now.
VK wrote:
Adam wrote:
I'm trying to retrieve some values from a json object, but instead it's
giving me the property name. For example:

var json = { "glossary": { "title": "example glossary" } };
console.log(json);
alert(json.glossary.title);
for (var x in json) { console.log(x); alert(x.title); }

This will show me the json object in the console with glossary and
title underneath it. When the alert for json.glossary.title fires, it
will show me "example glossary". however, inside of the for loop, I
see "glossary" in the console log (instead of the glossary object) and
undefined for the x.title alert.

What's going on? Could someone please explain why it's behaving this
way?

That will become much clearer if you try alert(typeof x); which gives
you "string".

That is a common misconception about for-in loop that it returns
enumerable *properties* of the object. I saw it written even in some
overall good manuals.

for-in loops returns string *names* of enumerable properties in the
given object.

var json = { "glossary": { "title": "example glossary" } };
alert(json);
alert(json.glossary.title);
for (var x in json) { alert(x); alert(json[x].title); }
Dec 7 '06 #3
I'm trying to retrieve some values from a json object, but instead it's
giving me the property name. For example:

var json = { "glossary": { "title": "example glossary" } };
console.log(json);
alert(json.glossary.title);
for (var x in json) { console.log(x); alert(x.title); }

This will show me the json object in the console with glossary and
title underneath it. When the alert for json.glossary.title fires, it
will show me "example glossary". however, inside of the for loop, I
see "glossary" in the console log (instead of the glossary object) and
undefined for the x.title alert.

What's going on? Could someone please explain why it's behaving this
way?
This might be helpful: http://yuiblog.com/blog/2006/09/26/for-in-intrigue/
Dec 7 '06 #4

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

Similar topics

3
by: arun32581 | last post by:
I am developing a page for Mozilla/IE which reads xml data and when the link on the page is clicked it displays the data as a table. The display is controlled by a Javascript. Everything works...
2
by: kungfumike | last post by:
I am currently having an issue getting json to work. The version of json,js I am using can be found here: http://www.json.org/json.js The file is innclided in standard html <script...
2
by: phil.swenson | last post by:
I'm using Prototype.js and would like to convert the contents of an HTML table to JSON. Converting to an array first is fine too. Any thoughts on this? I haven't seen anyone do anything this.......
6
by: dd | last post by:
I'm writing something in JS using the latest OO and JSON and I'm looking for a bit of guidance. I'm going to have this large object which has many top-level properties and some top-level...
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
4
by: im12345 | last post by:
I have the following question: Im doing a sample application using dojo and json. I have 2 classes: 1. Book class package com.esolaria.dojoex; import org.json.JSONObject; import...
7
by: Logos | last post by:
I am using PHP with the JSON extension function json_decode. I have a JSON with a member named "1" (ie) { "1":"somedata" } Trying to access this via the -operator doesn't work, nor does . ...
9
by: BryanA | last post by:
Is it possible to parse JSON data and then post it to a mysql db with the data in their respective fields?
0
by: JonTwend | last post by:
I am working with the Twitter API and finding it great fun and generally easy to use. But I am having trouble getting to the data in the trends/current.json file. I am able to loop through the...
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.