mysql - Coldfusion Lucee 4.5.2.018 (Linux) - REST 服务(无法转换字符串)JSON

标签 mysql web-services coldfusion railo lucee

从事 REST 网络服务,我没有太多冷融合网络服务的经验。这是非常基本的网络服务。如果你们能指出我,我做错了什么。这将是很大的帮助。

应用服务器:Lucee 4.5.2.018 (Linux)

请在下面找到我的代码。

组件功能/网络服务。

<cfcomponent rest="true" restpath="/hello">

    <cffunction name="formPost" access="remote" returnType="struct" httpMethod="POST" restPath="/name" hint="POST Method" produces="application/json">
           <cfargument name="firstname" type="String" restArgSource="Form">
           <cfargument name="lastname" type="String" restArgSource="Form">
         <cfset myStruct =  structnew()>  
           <cfset myStruct.FirstName = firstname>
           <cfset myStruct.LastName  = lastname>

            <cfquery name="Qry" datasource="myDSN">
                select col1,col2 from myTableData
            </cfquery>
           <cfset myJsonVar = serializeJSON(Qry) />
           <cfreturn myJsonVar>
    </cffunction>
</cfcomponent>

调用网络服务

<cfhttp url="http://mydev:8888/rest/Example/hello/name" method="POST"  result="res"  port="8888" >
        <cfhttpparam type="header" name="Accept" value="application/json">
        <cfhttpparam type="formfield" name="firstname" value="Dan">
        <cfhttpparam type="formfield" name="lastname" value="Gates">
</cfhttp>
<cfdump var="#res#">

问题: 定义 returnType="struct" 时错误 string can't cast String [{"COLUMNS":["COL1","COL2"],"DATA":[["0","7777777"],["0","888888"]]}] to a value of type [struct]

定义returnType="string"时没有报错"{\"COLUMNS\":[\"COL1\",\"COL2\"],\"DATA\":[[\"0\",\"7777777\"],[\"0\",\"888888\"]]}"

尝试在循环中获取 [DATA] 值

<cfloop from="1" to="#ArrayLen(d.DATA)#" index="i"> <cfloop from="1" to=#ArrayLen(d.DATA[i])# index="j"> <cfset resultSrt =d.COLUMNS[j]&" = " &d.DATA[i][j]> #resultSrt#<br> </cfloop> </cfloop>

留言:No matching property [DATA] found in [string] 堆栈跟踪:The Error Occurred in /opt/lucee/tomcat/webapps/ROOT/calling.cfm: line 52 50: 51: 52: <cfloop from="1" to="#ArrayLen(d.DATA)#" index="i"> 53: <cfloop from="1" to=#ArrayLen(d.DATA[i])# index="j"> 54: <cfset resultSrt =d.COLUMNS[j]&" = " &d.DATA[i][j]> Output

最佳答案

首先,由于您要返回查询,因此应将 returnType 设置为 Query

如果你已经将cffunctionproduces属性设置为application/json,那么你不需要执行显式的JSON序列化返回数据时。 ColdFusion 会自动为您完成。你可以只写:

<cfreturn Qry />

要读取服务返回的结果,您需要反序列化数据。像这样:

<cfdump var="#deserializeJson(res.filecontent)#"> 

关于mysql - Coldfusion Lucee 4.5.2.018 (Linux) - REST 服务(无法转换字符串)JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34006175/

相关文章:

coldfusion - ColdFusion 9 标准版获取正在运行的请求列表

php - 如何使用 PHP 将数据从 MySQL 提取到行中?

android - Web 服务、Android 客户端、SSL

java - 没有数据时应该返回哪个http代码

java - SOAPAction header 值异常 java web 服务

coldfusion - 了解 ColdFusion FindNoCase

coldfusion - 谷歌跨域认证

mysql - 如果表中的列具有某个特定值,如何查询 DBName

php - 我如何从不同页面中选择的列表中获取 ID?使用 PHP/MySQL

php - 检查具有多个输入的数据库表