工作在REST網絡服務上,我沒有太多的經驗coldfusion web-services.It是非常基本的web服務。請如果你們可以指點我,我做錯了什麼。這將是很大的幫助。Coldfusion Lucee 4.5.2.018(Linux) - REST服務(不能強制轉換字符串)JSON
應用服務器:Lucee 4.5.2.018(Linux)的
請在下面找到我的代碼。
組件功能/ Web服務。
<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>
調用Web服務
<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\"]]}"
試圖獲得環[數據]值
<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]>
不是你序列化一個對象作爲JSON字符串嗎?如果是這樣,ReturnType應該是「字符串」。您也可以將ReturnType設置爲「JSON」,而不是使用SerializeJSON函數。根據CF的版本,你可能也想考慮使用JSONUtil https://github.com/CFCommunity/jsonutil –
我只是在做serializeJSON()它會返回字符串嗎?我不會在任何地方投射我也試過返回時JSON沒有工作 – IBM