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

adding to existing obj using JSON

There's an object:

foo = new Object();

and a property called 'bar' with a value of 1 is to be added to it using
JSON. If I try the following it only demonstrates my ignorance:

var str = "{foo:{bar:1}}";
eval(str);
alert(foo.bar) // gives undefined

Could someone please explain what I'm doing wrong here?
Andrew Poulos
Jul 16 '06 #1
1 1741

Andrew Poulos wrote:
There's an object:

foo = new Object();

and a property called 'bar' with a value of 1 is to be added to it using
JSON. If I try the following it only demonstrates my ignorance:

var str = "{foo:{bar:1}}";
eval(str);
alert(foo.bar) // gives undefined
What you are trying to do is the equivalent of initialising a variable
with an object literal:

var foo = {bar : 1};
Presuming you are using AJAX or similar to return JSONtext, then
depending on how you want do it, you can do:

var JSONtext = '{bar:1}';
var foo = eval( '(' + JSONtext + ')' );

or

eval( 'var foo = ' + JSONtext );
Maybe you want to do:

var JSONtext = 'var foo = {bar:1}';
eval(JSONtext);
Over to you...
--
Rob

Jul 17 '06 #2

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... ...
1
by: routeslip | last post by:
Hi. I'm pretty experienced with Javscript but totally new to JSON and have a question... Is it possible to use JSON to "populate" objects that are already defined? For instance, I have two...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
1
by: Andrew Burton | last post by:
I'm poking at a small "single page application" (SPA), ala TiddlyWiki, to act as kind of a local, single-user version of Twitter (no real application, except to familiarize myself with JavaScript)....
1
by: Andrew Poulos | last post by:
I've just added JSON handling to my work and the code from json.org to create/parse the strings. The "compressed" version of the code starts: if(!Object.prototype.toJSONString){Array... This...
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...
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: 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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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.