Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-05-2011, 03:07 PM   PM User | #1
anjibman
New to the CF scene

 
Join Date: Oct 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
anjibman is an unknown quantity at this point
Converting custom Object to JSON object

Hi All,

I am trying to send the query result to view in JSON format. I am using DAO pattern so I have Transfer Object representing the table and my query create collection of such object from the result set. Until now I have been sending this collection to view through request object.

Now I am looking a way so that I can convert this collection to JSON objects in less code.

Thanks,
Anjib
anjibman is offline   Reply With Quote
Old 10-06-2011, 01:35 PM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
But in javaScript a custom object is a JSON object. I don't sense your request. Can you detail your aim, please? With some code examples, if possible.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 10-07-2011, 04:01 PM   PM User | #3
anjibman
New to the CF scene

 
Join Date: Oct 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
anjibman is an unknown quantity at this point
I am using DAO pattern in Struts 1.3.8 so
1. I have method Collection getAll() in DAO
public Collection getAll(){
.................
Collection coll = new ArrayList();
ResultSet rs = stmtSelect.executeQuery();
while(rs.next()){
//Read all columns and add to Collection 'coll'
}
return coll;
}

2. Then in my exuecute() method of Action class I just get that collection
execute(){
MyDAO daoObj = new MyDAO();
Collection myResults = daoObj.getAll();
//I need to convert this result to JSONObject to pass to view
}

Thanks
anjibman is offline   Reply With Quote
Old 10-24-2011, 09:36 AM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Use the method JSON.stringify(object_reference):
https://developer.mozilla.org/en/JSON
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 10-24-2011, 06:14 PM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
you can define .toJSON() methods for any enumerable property type that doesn't have a usable toString() for JSON, like Function or Element...


for example:

Code:
//new serializer methods:
Function.prototype.toJSON=function(){return this+'';}
Element.prototype.toJSON=function(){return this.value||this.text||this.textContent||this.innerHTML||"";}

//data to be serialized:
var o={
  num: 1, 
  fun: function me(){return this;},
  dom: document.getElementsByTagName("title")[0]
};

//test of json serialization with custom method:
alert(JSON.stringify(o, null, "\t"));
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%

Last edited by rnd me; 10-24-2011 at 06:19 PM..
rnd me is offline   Reply With Quote
Reply

Bookmarks

Tags
dao, databasee, json, query, resultset

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:07 AM.


Advertisement
Log in to turn off these ads.