473,573 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(jso n);
alert(json.glos sary.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.t itle 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 10163
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(jso n);
alert(json.glos sary.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.t itle 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.glos sary.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(jso n);
alert(json.glos sary.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.t itle 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.glos sary.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(jso n);
alert(json.glos sary.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.t itle 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
1568
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 fine. But in the table one of the sections is a URL. I want this to be clickable so that it takes me to the corresponding webpage. I am new to this and...
2
5494
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 src=json.js></script> fashion long story short. I have a function which will cycle through an associative array with a for loop and print out each key val...
2
6859
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.... thanks phil
6
2162
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 functions. That part I have no problem with, I'll define it in JSON style (my prototype is working just fine). What I'll also have though, is some arrays...
4
2811
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 great deal of help from gits, I've developed a DOM creation and deletion script, which can be used in multiple applications. You simply feed the...
4
8861
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 org.json.JSONException;
7
3040
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 . Putting the JSON into a foreach loop DOES access the member: foreach($json as $key=>$value) {
9
2215
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
2484
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 object data with a foreach loop and print it out. But I want to do more with the data than just print it. Just to be clear, I want to work with the...
0
7701
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8029
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8204
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5600
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5293
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3735
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2222
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.