如何将JSON数据作为正确的数据类型调用?

时间:2015-02-03 15:20:17

标签: json coldfusion

问题:使用ColdFusion从数据库调用JSON数据时,我使用什么数据类型?

后台:我的应用程序需要从数据库中提取一些JSON数据并将数据解析为HTML表单。

到目前为止,我的代码如下:

<cftry>
    <cfinvoke component="UserData.cfc.data" method="editData" returnvariable="editReturn">
            <cfinvokeargument name="formID" value="#URL.dataID#">
    </cfinvoke> 

    <cfset ReBuild = DeserializeJSON(#editReturns#)>
<cfcatch type="Any">
        <cfoutput>
            <hr>
            <h1>Other Error: #cfcatch.Type#</h1>
            <ul>
                <li><b>Message:</b> #cfcatch.Message#
                <li><b>Detail:</b> #cfcatch.Detail#
            </ul>
        </cfoutput>
        <cfset errorCaught = "General Exception">
    </cfcatch>

</cftry>

UserData.cfc.data:

    <cffunction name="editData" access="public" returntype="any">


        <cfargument name="formID" required="yes">


        <!--- Select --->
            <cfquery name="UserData" datasource="RC">
                SELECT      Data
                FROM        USER_Forms
        WHERE       ID = <cfqueryparam value="#ARGUMENTS.formID#">
            </cfquery>
<!-- The information pulled from the database should be a Serialized JSON data. -->
        <cfreturn UserData>

    </cffunction>

错误消息:

    Other Error: Expression

Message: Complex object types cannot be converted to simple values.
Detail: The expression has requested a variable or an intermediate expression result as a simple value. However, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values.
The most likely cause of the error is that you tried to use a complex value as a simple one. For example, you tried to use a query variable in a cfif tag.

当我将数据添加到数据库时,我使用了以下过程:

<cfset ForDBInsert = SerializeJSON(formCopy)>
<!-- Then I INSERTED ForDBInsert into the database columnn.  -->

1 个答案:

答案 0 :(得分:2)

尝试DeserializeJSON(editReturns.data) - 请注意,我以这种方式传递参数时,不需要#。看起来您正在尝试反序列化整个查询对象而不是字符串本身。