ColdFusion 9.0 Resources |
DeserializeJSONDescriptionConverts a JSON (JavaScript Object Notation) string data representation into CFML data, such as a CFML structure or array. See alsoIsJSON, SerializeJSON, cfajaxproxy, Using Ajax Data and Development Features in the Developing ColdFusion Applications, http://www.json.org Parameters
UsageThis function is useful any time a ColdFusion page receives data as JSON strings. It is useful in ColdFusion applications that use Ajax to represent data on the client browser, and lets you consume on the server JSON format data from the client-side Ajax JavaScript. You can also use it on pages that get data from services that supply data as JavaScript function calls with JSON parameters; the example shows this use case. The DeserializeJSON function converts each JSON data type directly into the equivalent ColdFusion data type, as follows:
ExampleThis example displays weather information from a JSON-format data feed that is generated by the example for the SerializeJSON function. Similar code might consume data that is exported as a JavaScript page. The feed is in the form of a JavaScript function call where the parameter is a JSON string that contains the feed data. The example does the following operations:
To run this example, put this file and the example for the SerializeJSON function in an appropriate location under your ColdFusion web root, replace the URL with the correct URL for the serialization example, and run this page. <!--- Get the JSON Feed ---> <cfhttp url="http://localhost:8500/My_Stuff/Ajax/Books/CreateJSON_NEW.cfm"> <!--- JSON data is sometimes distributed as a JavaScript function. The following REReplace functions strip the function wrapper. ---> <cfset theData=REReplace(cfhttp.FileContent, "^\s*[[:word:]]*\s*\(\s*","")> <cfset theData=REReplace(theData, "\s*\)\s*$", "")> <!--- Test to make sure you have JSON data. ---> <cfif !IsJSON(theData)> <h3>The URL you requested does not provide valid JSON</h3> <cfdump var="#theData#"> <!--- If the data is in JSON format, deserialize it. ---> <cfelse> <cfset cfData=DeserializeJSON(theData)> <!--- Parse the resulting array or structure and display the data. In this case, the data represents a ColdFusion query that has been serialized by the SerializeJSON function into a JSON structure with two arrays: an array column names, and an array of arrays, where the outer array rows correspond to the query rows, and the inner array entries correspond to the column fields in the row. ---> <!--- First, find the positions of the columns in the data array. ---> <cfset colList=ArrayToList(cfData.COLUMNS)> <cfset cityIdx=ListFind(colList, "City")> <cfset tempIdx=ListFind(colList, "Temp")> <cfset fcstIdx=ListFind(colList, "Forecasts")> <!--- Now iterate through the DATA array and display the data. ---> <cfoutput> <cfloop index="i" from="1" to="#ArrayLen(cfData.DATA)#"> <h3>#cfData.DATA[i][cityIdx]#</h3> Current Temperature: #cfData.DATA[i][tempIdx]#<br><br> <b>Forecasts</b><br><br> <cfloop index="j" from="1" to="#ArrayLen(cfData.DATA[i][fcstIdx])#"> <b>Day #j#</b><br> Outlook: #cfData.DATA[i][fcstIdx][j].WEATHER#<br> High: #cfData.DATA[i][fcstIdx][j].HIGH#<br> Low: #cfData.DATA[i][fcstIdx][j].LOW#<br><br> </cfloop> </cfloop> </cfoutput> </cfif> |