ColdFusion, AJAX and web...
<response>
<result>0</result>
<resultstring>OK</resultstring>
<data>
<struct>
<firstname>John</firstname>
<lastname>Smith</lastname>
</struct>
</data>
</response>
Using JSON to describe the data:
<response>
<result>0</result>
<resultstring>OK</resultstring>
<data>{ firstname:"John", lastname:"Smith" }</data>
</response>
As you can see, the JSON version is MUCH smaller than its more verbose counterpart. This is even more true when you are considering an array of structures, or complex structures.
How ColdFusion variables were converted into JSON
Firstly, we created what we called a Request Broker. I'll go into more detail about this in a later post, but it essentially took the component and method as arguments and returned an XML document, with the ColdFusion variable returned by the method encoded as JSON.
The Request Broker would get called like this:
http://www.example.com/requestBroker.cfm?class=componentPath&method=get
The ColdFusion tempalte dynamically evaluates the method passed via the URL, and also passes the FORM scope as an argument. On the client side, name/value pairs are always passed as a POST instead of a GET. This is because some variables sent to the server might be large lists or text.
<cfscript>
o = createObject("component", url.class);
cfData = o[url.method](FORM);
jsonData = jsonencode(cfData.data);
</cfscript>
It is the job of the component (url.class) to appropriately extract the arguments from the FORM data and then pass them as individual parameters to the Model.
The output of the method is then encoded using the CFJSON UDF, written by Jehiah, and then wrapped up in an XML envelope.
<response>
<result>
#cfData.result#
</result>
<resultstring>#cfData.resultString#</resultstring>
<data>#
jsonData
#</data>
</response>
In my next post I'll cover the Request Broker in more detail, as well as provide a full source-code description. The idea of this post was to show you the differences between XML and JSON, and how we converted ColdFusion variables into JSON.
Nicely Done ! Great....
we had lots of metadata (attributes) to send back with the values and that's why we were stuck with XML. no help from JSON with this, yes?
I take it you only needed the data (re: "xml bloat")?
By zwetan, at 1/03/2006 08:32:00 am
hi, check also this post
The AJAX response: XML, HTML, or JSON?
the article and comments are interesting and in the same vein as your post.
What is the deal with JSON.Com? Is it like JSON.Org or Xml.Com?
By Darryl Lyons, at 1/03/2006 06:46:00 pm
Barry - as long as you can represent the information in a ColdFusion complex structure, then you can certainly do so in JSON. JSON allows you to build complex nested data -- e.g. array of structs, structs of arrays, etc.
zwetan - A good read. I do agree that JSON is harder to read than XML, but if the JSON is being created by a machine in the first place, then I don't think it matters once it is use. Another interesting thing is that we also used HTML snippets.
By Unknown, at 6/24/2010 01:34:00 pm
You would become acclaimed in your acquisition just because of accustomed the best superior lv bags
with you; these cheap louis vuitton bags
are accepting added popularity. Wherever you would go, these lv bag
would be taken as a attribute of chic and elegance.
By 123, at 8/28/2010 04:01:00 pm
http://bookmarkcircle.info/user/voted/dvd123/
http://bookmarkcircle.info/user/voted/hardysed123/
http://bookmarkcircle.info/user/voted/hermes123/
http://bookmarkcircle.info/user/voted/hermes321/
http://bookmarkco.com/user.php?login=bootpoint&view=history
http://bookmarkco.com/user.php?login=freeboots&view=history
http://bookmarkco.com/user.php?login=lv123&view=history
http://bookmarkco.com/user.php?page=3&login=tiffany123&view=history
http://bookmarkin.info/user.php?login=breitling123&view=voted
http://bookmarkin.info/user.php?login=Chanel123&view=voted