【问题标题】:Coldfusion Lucee 4.5.2.018 (Linux) - REST service (can't cast String) JSONColdfusion Lucee 4.5.2.018 (Linux) - REST 服务(不能转换字符串)JSON
【发布时间】:2016-03-04 12:51:40
【问题描述】:

在 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]>

【问题讨论】:

  • 你不是将对象序列化为 JSON 字符串吗?如果是这样,ReturnType 应该是“字符串”。您还可以将 ReturnType 设置为“JSON”而不使用 SerializeJSON 函数。根据 CF 的版本,您可能还需要考虑使用 JSONUtil github.com/CFCommunity/jsonutil
  • 我只是在做 serializeJSON() 它返回字符串吗?我没有在任何地方投射。我也试过返回时间 JSON 不起作用

标签: mysql web-services coldfusion railo lucee


【解决方案1】:

首先,既然你要返回一个查询,你应该将returnType设置为Query

如果您已将cffunctionproduces 属性设置为application/json,在这种情况下,您无需在返回数据时执行显式JSON 序列化。 ColdFusion 会自动为您完成。你可以写:

<cfreturn Qry />

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

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

【讨论】:

  • 感谢您的回复,我按照您所说的做了仍然给我字符串输出。让我清楚一点,在 CF10 应用程序服务器中运行相同的代码时,它正在工作,发现它给了我返回数组
  • 我照你说的做了,仍然给我字符串输出我完全按照 Pankaj 的描述修改了你的代码,它与 Lucee 完美配合。我怀疑你忘了刷新,或者类似的东西。
  • @Leigh 我刚刚上传了我的输出它的显示字符串。我想要数据...我怎样才能做单独的数据?我应该使用 find() 函数来选择我需要的值吗?
  • 我的输出它的显示字符串 输出仍然是错误的。当您看到引号被 \ 转义时,例如 {\"COLUMNS\":...,这通常意味着您错误地对结果进行了两次编码。你的代码有些不同。如果您应用 Pankaj 建议的步骤 - 仔细(一次一个),它将起作用。 1) 将 returnType 设置为 Query。 2) 删除 serializeJSON 并返回 Qry根据需要刷新网络服务 3) 反序列化响应字符串deserializeJson(res.filecontent)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 2016-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多