Converts ColdFusion data into a JSON (JavaScript Object Notation) representation of the data.
A string that contains a JSON representation of the parameter value.
SerializeJSON(var
[,serializeQueryByColumns
])
DeserializeJSON, IsJSON, cfajaxproxy, "Using data interchange formats" in the ColdFusion Developer's Guide, http://www.json.org
ColdFusion 8: Added function
Parameter |
Description |
---|---|
var |
A ColdFusion data value or variable that represents one. |
serializeQueryByColumns |
A Boolean value that specifies how to serialize ColdFusion queries.
For more information, see the Usage section. |
This function is useful for generating JSON format data to be consumed by an Ajax application.
The SerializeJSON function converts ColdFusion dates and times into strings that can be easily parsed by the JavaScript Date object. The strings have the following format:
MonthName, DayNumber Year Hours:Minutes:Seconds
The SerializeJSON function converts the ColdFusion date time object for October 3, 2007 at 3:01 PM, for example, into the JSON string "October, 03 2007 15:01:00".
The SerializeJSON function with a false serializeQueryByColumns parameter (the default) converts a ColdFusion query into a row-oriented JSON Object with the following elements:
Element |
Description |
---|---|
COLUMNS |
An array of the names of the columns. |
DATA |
A two-dimensional array, where:
|
For example, the SerializeJSON function with a serializeQueryByColumns parameter value of false converts a ColdFusion query with two columns, City, and State, and two rows of data into following format:
{"COLUMNS":["CITY","STATE"],"DATA":[["Newton","MA"],["San Jose","CA"]]}
The SerializeJSON function with a serializeQueryByColumns parameter value of true converts a ColdFusion query into a column-oriented JSON Object that is equivalent to the WDDX query representation. The JSON Object has three elements:
Element |
Description |
---|---|
ROWCOUNT |
The number of rows in the query. |
COLUMNS |
An array of the names of the columns. |
DATA |
An Object with the following:
|
The SerializeJSON function with a serializeQueryByColumns parameter value of true converts a ColdFusion query with two columns, City, and State, and two rows of data into following format:
{"ROWCOUNT":2, "COLUMNS":["CITY","STATE"],"DATA":{"City":["Newton","San Jose"],"State":["MA","CA"]}}
The SerializeJSON function converts all other ColdFusion data types to the corresponding JSON types. It converts structures to JSON Objects, arrays to JSON Arrays, numbers to JSON Numbers, and strings to JSON Strings.
This example creates a JSON-format data feed with simple weather data for two cities. The data feed is in the form of a JavaScript application that consists of a single function call that has a JSON Object as its parameter. The example code does the following:
If you view this page in your browser, you see the resulting JavaScript function and JSON parameter. To use the results of this page in an application, put this file and the example for the DeserializeJSON function in an appropriate location under your ColdFusion web root, replace the URL in the DeserializeJSON example code with the correct URL for this page, and run the DeserializeJSON example.
<!--- Generate a clean feed by suppressing white space and debugging information. ---> <cfprocessingdirective suppresswhitespace="yes"> <cfsetting showdebugoutput="no"> <!--- Generate the JSON feed as a JavaScript function. ---> <cfcontent type="application/x-javascript"> <cfscript> // Construct a weather query with information on cities. // To simplify the code, we use the same weather for all cities and days. // Normally this information would come from a data source. weatherQuery = QueryNew("City, Temp, Forecasts"); QueryAddRow(weatherQuery, 2); theWeather=StructNew(); theWeather.High=73; theWeather.Low=53; theWeather.Weather="Partly Cloudy"; weatherArray=ArrayNew(1); for (i=1; i<=5; i++) weatherArray[i]=theWeather; querySetCell(weatherQuery, "City", "Newton", 1); querySetCell(weatherQuery, "Temp", "65", 1); querySetCell(weatherQuery, "ForeCasts", weatherArray, 1); querySetCell(weatherQuery, "City", "San Jose", 2); querySetCell(weatherQuery, "Temp", 75, 2); querySetCell(weatherQuery, "ForeCasts", weatherArray, 2); // Convert the query to JSON. // The SerializeJSON function serializes a ColdFusion query into a JSON // structure. theJSON = SerializeJSON(weatherQuery); // Wrap the JSON object in a JavaScript function call. // This makes it easy to use it directly in JavaScript. writeOutput("onLoad( "&theJSON&" )"); </cfscript> </cfprocessingdirective>