Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Kor
    Kor is offline
    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
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    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"));
    Last edited by rnd me; 10-24-2011 at 06:19 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •