473,471 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

JSON as datasource for prototype objects

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
prototpye objects. I try to give an example. I e.g. got:

address.prototype =
{
firstname: null;
lastname: null;
email: null;
//imagine a lot more properties here

getfirstname: function () { return this.firstname; },
checkadress: function() {//some code here;}
}

Imagine address has a lot of properties and methods. Now I want to use
JSON to create a new address-object and initialize the properties with
the data supplied in the JSON Object. How can I do this without having
to write a lot of code? What would the JSON Object have to look like?
Best,

Hendrik

Aug 16 '07 #1
2 2491
There might be a better way, but the first thing I thought was a
function to pass additional data to the original definition.

Example:

var jsonObject = {
foo: 'foo',
bar: 'bar',

foo: function() {
alert(this.foo);
},

loadData: function(jsonData) {
for(index in jsonData) {
this[index] = jsonData[index];
}
},
};

//jsonObject.foo() ="foo"
//jsonObject.bar ="bar"

jsonObject.loadData( {
bar: function() {
alert(this.bar);
},

bar: 'foobar'
} );

//jsonObject.bar() ="foobar"
On Aug 16, 11:45 am, holtm...@gmail.com wrote:
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
prototpye objects. I try to give an example. I e.g. got:

address.prototype =
{
firstname: null;
lastname: null;
email: null;
//imagine a lot more properties here

getfirstname: function () { return this.firstname; },
checkadress: function() {//some code here;}

}

Imagine address has a lot of properties and methods. Now I want to use
JSON to create a new address-object and initialize the properties with
the data supplied in the JSON Object. How can I do this without having
to write a lot of code? What would the JSON Object have to look like?

Best,

Hendrik

Aug 17 '07 #2
Sorry,

I misread, here is what you're probably looking for.

var jsonObject = function() {
this.bar = 'bar';

this.foo = function() {
alert(this.bar);
};
}
jsonObject.prototype = {
loadData: function(jsonData) {
for(index in jsonData) {
this[index] = jsonData[index];
}
}
};

var jObj = new jsonObject();

jObj.foo(); // ="bar"

jObj.loadData( {
bar: function() {
alert('foobar');
}
} );

jObj.bar(); // ="foobar"
On Aug 16, 9:51 pm, aonic <raja.ka...@gmail.comwrote:
There might be a better way, but the first thing I thought was a
function to pass additional data to the original definition.

Example:

var jsonObject = {
foo: 'foo',
bar: 'bar',

foo: function() {
alert(this.foo);
},

loadData: function(jsonData) {
for(index in jsonData) {
this[index] = jsonData[index];
}
},

};

//jsonObject.foo() ="foo"
//jsonObject.bar ="bar"

jsonObject.loadData( {
bar: function() {
alert(this.bar);
},

bar: 'foobar'

} );

//jsonObject.bar() ="foobar"

On Aug 16, 11:45 am, holtm...@gmail.com wrote:
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
prototpye objects. I try to give an example. I e.g. got:
address.prototype =
{
firstname: null;
lastname: null;
email: null;
//imagine a lot more properties here
getfirstname: function () { return this.firstname; },
checkadress: function() {//some code here;}
}
Imagine address has a lot of properties and methods. Now I want to use
JSON to create a new address-object and initialize the properties with
the data supplied in the JSON Object. How can I do this without having
to write a lot of code? What would the JSON Object have to look like?
Best,
Hendrik


Aug 17 '07 #3

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

Similar topics

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: 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...
9
by: eggie5 | last post by:
eval is not working on this JSON string: {"top":} {"car":} {"man":} data = eval('(' + json + ')'); What's wrong with the json string, is it 'cause it has no root?
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: 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...
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...
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: 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
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
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
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.