473,756 Members | 6,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Complex JSON obj, best strategy?

dd
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 at the top level.
These arrays will contain n number of sub-objects. Each
of these sub-objects will also have properties and arrays
of their own. Again, no problem so far.

What I would like advice on is the strategy for adding
functions to these sub-objects in the most efficient way.

Let's say I have an array of 10 sub-objects, it would be
highly inefficient to define the function inline in each of the
10 sub-object definitions. I'm thinking it's probably better
to declare these things in a loop after the end of the
main object definition, something like this:

for(var i=0;i<mainobj.s ubobjarray.leng th;i++)
mainobj.subobja rray[i].somefunc=funct ion(){bla();}

Obviously that func definition wouldn't be a one-liner.

Also, does anyone have a good suggestion for how best
to align JSON structure definitions? Here's the kind of
thing I have so far:

mainobj = {
id:123,
name:"fred",
type:"whatever" ,
toplevelfunc:fu nction(){do_stu ff();},
toplevel2:funct ion(){do_more_s tuff();},
subobjarray:
[ { num:0,
name:"hello",
foo:"bar",
func_here:funct ion(){is_a_bad_ idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
},
{ num:1,
name:"hello",
foo:"bar",
func_here:funct ion(){is_a_bad_ idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
}
],
final_property: "the_end",
final_func:func tion(){alert("r eally the end");
};

//add functions into the arrays of sub-obj's here.

Jun 20 '07 #1
6 2179
dd
On Jun 20, 5:05 pm, dd <dd4...@gmail.c omwrote:
],
final_property: "the_end",
final_func:func tion(){alert("r eally the end");

};
Yeah I know my closing braces got screwed
up at the end, I think it's google groups
that did that.

Jun 20 '07 #2
On Jun 20, 11:09 am, dd <dd4...@gmail.c omwrote:
On Jun 20, 5:05 pm, dd <dd4...@gmail.c omwrote:
],
final_property: "the_end",
final_func:func tion(){alert("r eally the end");
};

Yeah I know my closing braces got screwed
up at the end, I think it's google groups
that did that.
I think you should break thinks apart into different functions and use
new to construct the objects. Or you can have the functions return an
object. Whichever style you prefer.

function MySubObj(num) {
this.num = num;
this.name = 'hello';
this.foo = 'bar';

this.afunction = function() {
// much easier to define here.
}

// and so on
}

function MyObj() {
this.id = 123;
this.name = 'fred';

this.subObjarra y = [new MySubObj(1), new MySubObj(2)];

// and so on.
}

var mainobj = new MyObj();

Jun 21 '07 #3
dd
On Jun 21, 2:40 pm, Jang <jangc...@gmail .comwrote:
I think you should break thinks apart into different
functions and use new to construct the objects.
Thanks for the suggestion. That would work nicely
but my sub-objects will have 30+ properties and I
don't want to pass them all in via the constructor
call. I tried to avoid showing that much detail in
the example. In the sub-sub-object cases where
there's not too much data I'd need to pass in thru
the constructor I will do that though :)

Jun 21 '07 #4
dd wrote:
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).
JSON is a data transmission/exchange format based upon a subset of
javascript's object literal notation. JSON explicitly forbids the use of
functions as part of the data.
What I'll also have though, is some arrays at the top level.
These arrays will contain n number of sub-objects. Each
of these sub-objects will also have properties and arrays
of their own. Again, no problem so far.

What I would like advice on is the strategy for adding
functions to these sub-objects in the most efficient way.

Let's say I have an array of 10 sub-objects, it would be
highly inefficient to define the function inline in each of the
10 sub-object definitions. I'm thinking it's probably better
to declare these things in a loop after the end of the
main object definition, something like this:

for(var i=0;i<mainobj.s ubobjarray.leng th;i++)
mainobj.subobja rray[i].somefunc=funct ion(){bla();}
In terms of what the code did (particularly the number of function
objects created) that would be no more efficient than defining the
functions directly in the object literal notation. All you have done is
reduce the amount of source code.
Obviously that func definition wouldn't be a one-liner.
that is perhaps a pity as if all the function did was call another
function with no arguments you could write:-

mainobj.subobja rray[i].somefunc = bla;

- and have all these objects share the same reference to a single
function object.
Also, does anyone have a good suggestion for how best
to align JSON structure definitions? Here's the kind of
thing I have so far:
They are not JSON if they include functions, they are object literals.
mainobj = {
id:123,
name:"fred",
type:"whatever" ,
toplevelfunc:fu nction(){do_stu ff();},
toplevel2:funct ion(){do_more_s tuff();},
subobjarray:
[ { num:0,
name:"hello",
foo:"bar",
func_here:funct ion(){is_a_bad_ idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
},
{ num:1,
name:"hello",
foo:"bar",
func_here:funct ion(){is_a_bad_ idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
}
],
final_property: "the_end",
final_func:func tion(){alert("r eally the end");
};

//add functions into the arrays of sub-obj's here.


var mainobj = new MainObject(
123,
"fred",
"whatever",
[
new SubObject(
0,
"hello",
"bar",
[
new SubSubObject("b la","whateva!") ,
new SubSubObject("w hat?","hi")
]
),
new SubObject(
1,
"hello",
"bar",
[
new SubSubObject("b la","whateva!") ,
new SubSubObject("w hat?","hi")
]
)
],
"the_end",
);

function MainObject(name , type, subobjarray, final_property) {
this.name = name;
this.type = type;
this.subobjarra y = subobjarray;
this.final_prop erty = final_property;
};
MainObject.prot otype.toplevelf unc = function(){do_s tuff();};
MainObject.prot otype.toplevel2 = function(){do_m ore_stuff();};
MainObject.prot otype.final_fun c = function(){aler t("really the end");
function SubObject(num, name, foo, otherarray){
var c, len;
this.num = num;
this.name = name;
this.foo = foo;
this.otherarray = otherarray;
}
SubObject.proto type.func_here = function(){is_a _bad_idea();};

function SubSubObject(bl a,like ){
this.fbla = bla;
this.flike = like
}
SubSubObject.pr ototype.somefun c = function(){bla( );};

And the - mainobj - structure is still not JSON, as - new - is not
allowed in addition to functions.

Richard.

Jul 2 '07 #5
d d
Richard Cornford wrote:
dd wrote:
They are not JSON if they include functions, they are object literals.
Yeah, I seem to be using the term JSON wrongly. There's
no interchange going on between languages, via XML or
in any kind of server dialog. Really I just mean declaring
my objects/functions using the object notational style.
var mainobj = new MainObject(
123,
"fred",
"whatever",
[
new SubObject(
0,
"hello",
"bar",
[
new SubSubObject("b la","whateva!") ,
new SubSubObject("w hat?","hi")
]
),
new SubObject(
1,
"hello",
"bar",
[
new SubSubObject("b la","whateva!") ,
new SubSubObject("w hat?","hi")
]
)
],
"the_end",
);

function MainObject(name , type, subobjarray, final_property) {
this.name = name;
this.type = type;
this.subobjarra y = subobjarray;
this.final_prop erty = final_property;
};
MainObject.prot otype.toplevelf unc = function(){do_s tuff();};
MainObject.prot otype.toplevel2 = function(){do_m ore_stuff();};
MainObject.prot otype.final_fun c = function(){aler t("really the end");
function SubObject(num, name, foo, otherarray){
var c, len;
this.num = num;
this.name = name;
this.foo = foo;
this.otherarray = otherarray;
}
SubObject.proto type.func_here = function(){is_a _bad_idea();};

function SubSubObject(bl a,like ){
this.fbla = bla;
this.flike = like
}
SubSubObject.pr ototype.somefun c = function(){bla( );};

And the - mainobj - structure is still not JSON, as - new - is not
allowed in addition to functions.

Richard.
One of the major reasons I want to do this, is because
these sub objects have a lot of properties. I tried to
scale it down to an easily read example. The trouble is,
doing that made it seem that certain methods (like using
the new SubObject would be the best method.

As I haven't yet decided on how many methods and how
many data elements will be in the objects, I'll bear
in mind the benefits you outlined above to ensure I get
the best mix of inline definitions and pre-defined
objects.

Thanks !!

~dd
Jul 2 '07 #6
d d wrote:
Richard Cornford wrote:
>dd wrote:
They are not JSON if they include functions, they are object
literals.

Yeah, I seem to be using the term JSON wrongly. There's
no interchange going on between languages, via XML or
in any kind of server dialog. Really I just mean declaring
my objects/functions using the object notational style.
>var mainobj = new MainObject(
123,
"fred",
"whatever",
[
new SubObject(
0,
"hello",
"bar",
[
new SubSubObject("b la","whateva!") ,
new SubSubObject("w hat?","hi")
]
),
new SubObject(
1,
"hello",
"bar",
[
new SubSubObject("b la","whateva!") ,
new SubSubObject("w hat?","hi")
]
)
],
"the_end",
);

function MainObject(name , type, subobjarray, final_property) {
this.name = name;
this.type = type;
this.subobjarra y = subobjarray;
this.final_prop erty = final_property;
};
MainObject.pro totype.toplevel func = function(){do_s tuff();};
MainObject.pro totype.toplevel 2 = function(){do_m ore_stuff();};
MainObject.pro totype.final_fu nc = function(){aler t("really the end");
function SubObject(num, name, foo, otherarray){
var c, len;
this.num = num;
this.name = name;
this.foo = foo;
this.otherarray = otherarray;
}
SubObject.prot otype.func_here = function(){is_a _bad_idea();};

function SubSubObject(bl a,like ){
this.fbla = bla;
this.flike = like
}
SubSubObject.p rototype.somefu nc = function(){bla( );};

And the - mainobj - structure is still not JSON, as - new - is not
allowed in addition to functions.

Richard.

One of the major reasons I want to do this, is because
these sub objects have a lot of properties. I tried to
scale it down to an easily read example. The trouble is,
doing that made it seem that certain methods (like using
the new SubObject would be the best method.
<snip>

I don't think that really matters, more properties means more values -
more data. That would seem to favour the above as it increases the ratio
of date to mechanism code details, and keeps to two isolated from each
other. The only significant disadvantage is that the data is not
labelled in the context where it appears. Traded against the advantages
of having the mechanism defined once and in one place and inherited as a
side effect of creating the objects.

Richard.

Jul 2 '07 #7

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

Similar topics

20
6880
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... PS i am ready to use JSON as data/object interchange when using AJAX and my J2EE project - because it is easier to traverse the JavaScript object than its XML representation (so of course may argue).
5
1699
by: Dominic Myers | last post by:
In the full and frank knowledge that someone will doubtless refer me to google and probably tell me to wipe my own arse I was wondering if someone could shed some light on the following problem for me? I have a number of select blocks that I want to get the data from into a PHP script on the server (I know, sounds very AJAXie doesn't it?). I'm thinking that about the best way to do this would
13
12412
by: trpost | last post by:
I am looking to make a small web app that will return the status of a website from the client browser. I tried this with AJAX and it worked great locally, but did not work for remote users accessing the page, I ran into the security problem with making a cross domain request. I have been reading that with JSON a cross domain request can be accomplished, but have not been able to find any examples on how to use it or how to get the return...
4
4060
by: VK | last post by:
Google Trends is an all new service (started May 10) and I have not responsability for proper query or data accuracy. Overall seems pretty close to what could be observed by the post history in c.l.j. Just curious why exactly Japan got so exclusively hot on JSON ? <http://www.google.com/trends?q=AJAX+JavaScript&ctab=0&geo=all&date=all> <http://www.google.com/trends?q=JSON+JavaScript&ctab=0&geo=all&date=all>
2
3324
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 recently -- or maybe I should just speak for myself.) The fact that JSON is more elegant goes without saying, yet I can't seem to find a way to use JSON the way I *really* want to use it: to create objects that can be instantated into multiple...
5
3822
by: michal | last post by:
hi guys, i thought you might be interested in a nice JSON class which converts ASP datatypes (basic datatypes, dictionaries, recordsets, ...) into JSON so that javascript can easily understand it ... you'll find the demonstration and the download here http://fabiankoehler.de/wdb/2007/04/26/generate-json-from-asp-datatypes/
2
11453
by: giloosh | last post by:
whats the best way to pass a json string to the server. if my jsonstring = {a:'1',b:'sds',c:'sdg'} could i send that to the server passing it as 1 variable like so: url = /serverside.php?json=jsonstring or would i have to break it down into something like this: url = /serverside.php?a=1&b=sds&c=sdg
2
2518
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 prototpye objects. I try to give an example. I e.g. got: address.prototype = { firstname: null;
6
2829
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9844
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6514
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
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
2
3326
muto222
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.