Hi Patrick var json=$.params2json(myFormParams.vars); will return a hash (object). To convert a hash into a string, you will need something like json.js. document.getElementById('json').innerHTML=json.toJSONString(); should give you what you expect. However, if you merely want to submit the json to the server, you can do so without converting it to a string. Like so: $.post(url,json,callback); I don't know about the error in toJSONString. I am using a very old version of json.js which has a JSON.stringify(json) method, and that seems to work without trouble. Let me know if you need it, and I'll forward it to you. But like said before, unless you really need to read the JSON, you dont need to stringify it to submit the JSON.
Hope this helps Regards Ashutosh On 3/30/07, Patrick Barnes <[EMAIL PROTECTED]> wrote:
Thank you very much for your prompt reply. I am not, however, able to get this to work as I would expect. Using the JQuery.js lib I'm able to do the following: var myFormParams = $('#myForm').serialize(); If I then do the following I see "5", which is correct: document.getElementById('json').innerHTML=myFormParams.vars.length; But when I call the following I just see "[object Object]": document.getElementById('json').innerHTML=$.params2json(myFormParams.vars ); If I call the following (from the json.js script at JSON.org) I get "[{"name":"textbox1","value":"ddeast"},{"name":"textbox2","value":"west"},{"name":"drop_mode","value":"2"},{"name":"east_checkboxes","value":"delay_ena"},{"name":"","value":"Set UI"}]": document.getElementById('json').innerHTML=myFormParams.vars.toJSONString (); So this is what I'm after in the end, but I'd rather use JQuery entirely, so I was hoping you might tell me how to use your function properly. The toJSONString method also has an issue that Firebug flags (though it allows the function to run): Too much recursion a.push(k.toJSONString(), ':', s); This happens on Line 165 of the 2007-03-20 build. Thanks, Patrick On 3/29/07, ashutosh bijoor <[EMAIL PROTECTED]> wrote: > > Hi Patrick > > You're right. I have (had) not found it either, and had written this > utility to convert the serialized data into JSON, which you may find useful > - the name is a bit weird, but does the job: > > // @jsfn R121.SectionMgr.params2json(parray) > // Converts parameter array received from serializing the form into JSON > $.params2json = function(d) { > if (d.constructor != Array) { > return d; > } > var data={}; > for(var i=0;i<d.length;i++) { > if (typeof data[d[i].name] != 'undefined') { > if (data[d[i].name].constructor!= Array) { > data[d[i].name]=[data[d[i].name],d[i].value]; > } else { > data[d[i].name].push(d[i].value); > } > } else { > data[d[i].name]=d[i].value; > } > } > return data; > }; > > > On 3/30/07, Patrick Barnes <[EMAIL PROTECTED] > wrote: > > > > Hi Ashutosh, > > > > I'm using your excellent deserialize function to set UI elements via > > an asynch call to some JSON data via the Yahoo UI connection manager. I also > > really need to serialize the form elements to JSON. I am very new to JQuery. > > I looked everywhere for "Mark Constable's serialize function", which you > > mention, but I cannot find it. The closest I could find was formSerialize() > > at http://www.malsup.com/jquery/form/#api, but it only serializes to a > > querystring, not JSON. Any ideas on how I might do this? > > > > Thank you, > > > > Patrick > > > > > > -- > Reach1to1 Technologies > http://www.reach1to1.com > http://on2.biz > [EMAIL PROTECTED]
-- Reach1to1 Technologies http://www.reach1to1.com http://on2.biz [EMAIL PROTECTED]
_______________________________________________ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/