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

JSON with existing Objects

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 "classes":

function class1() {
this.class2_array;
}

function class2() {
this.prop1;
this.prop2;
this.prop3;
this.prop4;
}

I want to create a bunch of class2 objects in the class1.class2_array
property.

The existing classes already have their own methods, etc so it's not
just about storing data. Previously I created log signatures for class2
and basically created the objects the "old fashioned" way.

Can I do this?

Mar 23 '06 #1
1 1489


ro*******@gmail.com wrote:

Is it possible to use JSON to "populate" objects that are already
defined? For instance, I have two "classes":

function class1() {
this.class2_array;
}

function class2() {
this.prop1;
this.prop2;
this.prop3;
this.prop4;
}

I want to create a bunch of class2 objects in the class1.class2_array
property.


With JSON I think all you can do is pass e.g.
var obj = { 'prop1': 1, 'prop2': 2, 'prop3': 3, 'prop4': 4 }
around, you could iterate over the property names then and initialize
your "class2 instance"
var c2 = new class2();
for (var propertyName in obj) {
c2[propertyName] = obj[propertyName];
}
And for..in might enumerate properties added to Object.prototype so
depending on libraries you might use you need to take care with that
construct not to add properties defined on the prototype.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 23 '06 #2

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

Similar topics

16
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post...
1
by: Red Daly | last post by:
Hello group, I have been using JSON for a while and it has made many things a breeze. However, JSON does not natively describe certain things like pointers and custom types. I created a simple...
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...
5
by: Marc | last post by:
I'm aware that there are significant differences between VBScript objects and JScript objects but that doesn't mean something like the following should give me such troubles? <%@...
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...
2
by: holtmann | last post by:
Hi, I got a question regarding JSON as datasource- I understand that eval on a JSON String creates the appropriate objects in JS. But I would like to use JSON to supply data to already defined...
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...
1
by: Andrew Poulos | last post by:
I'm building some e-learning that's managed by an learning management system (LMS). The LMS allows only one "field" for me to store custom lesson data. The e-learning currently uses a number of...
1
by: bizt | last post by:
Hi, Im currently looking to move into using JSON for AJAX instead of returning from the server a string like the following: 12345{This is a text string{true I prefer objects because I dont...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
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...
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.