|
IsJSON
DescriptionEvaluates
whether a string is in valid JSON (JavaScript Object Notation) data interchange
format.
ReturnsTrue
if the parameter is a valid JSON value.
False if the parameter
is not a valid JSON data representation.
HistoryColdFusion
8: Added function
Parameters
Parameter
|
Description
|
var
|
A string or variable that represents one.
|
ExampleThis
example checks whether the data feed that is generated by the example
for the SerializeJSON function contains
valid JSON data.
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:
Uses
a cfhttp tag to get the feed (in the cfhttp.fileContent variable).
Strips the function call wrapper from the text.
Uses the IsJSON function to check whether
the result of the previous step is a valid JSON format string.
Displays a message that indicates whether the text is valid
JSON data, followed by the feed text string.
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 often 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 provides valid JSON</h3>
<cfelse>
<h3>The URL you requested does not provide valid JSON</h3>
</cfif>
<!--- Display the data. --->
<cfoutput>#theData#</cfoutput>
For a
more complex example that then converts the JSON data, see DeserializeJSON.
|