json - 返回 JSON 对象

标签 json rest coldfusion cfml

有没有办法通过ColdFusion中的函数返回真实的JSON对象? 我当前的解决方案是将查询转换为字符串,然后在另一个 CF 文件中将其转换回 JSON 对象:

<cffunction name="addLicense" access="remote" returntype="string" returnFormat="JSON" httpmethod="POST">
  <cfquery datasource="hostmanager" name="createCustomer">
    SELECT * FROM license
  </cfquery>
  <cfreturn serializeJSON(createCustomer)>
</cffunction>

最佳答案

使用serializeJSON有很多不同的方法

<cfscript>
    myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", 
                [ 
                        {id=1,name="One",amount=15}, 
                        {id=2,name="Two",amount=18}, 
                        {id=3,name="Three",amount=32} 
                ]); 
    writeOutput("The new query is:")
    writeDump(myQuery)
</cfscript>

<cfoutput>
    <h4>Default</h4>
    <p><code>#serializeJSON(myQuery)#</code></p>
    <h4>Row</h4>
    <p><code>#serializeJSON(myQuery, "row")#</code></p>
     <h4>Column</h4>
    <p><code>#serializeJSON(myQuery, "column")#</code></p>
     <h4>Struct</h4>
    <p><code>#serializeJSON(myQuery, "struct")#</code></p>
</cfoutput>

结果

enter image description here

参见:https://cffiddle.org/app/file?filepath=58f7cee2-dabb-42f8-91ef-7dd41e1691c0/49ad94cb-23e4-4c9c-9986-b7c1d4c15e3a/c818a99e-4476-4625-8bec-657bcfa9b0e2.cfm

关于json - 返回 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56599523/

相关文章:

javascript - Coldfusion 中提交的表单会发生什么?

java - 需要帮助从完整的 JSON 数组/对象中提取字符串

android - retrofit 2.0如何忽略json对象动态响应的key

c# - .NET Core API - 为什么嵌套模型中的文件属性接收 null?

java - SpringFramework HttpClientErrorException 400错误请求,如何在java端POST?

rest - IIS7.5 尝试使用 DELETE 动词时出现 500 内部服务器错误

html - <hr> 在 coldfusion cfdocument 中

python - 用于 Python 的 Lenient JSON 解析器

java - 通过 JSON 解析器在 Java 中拆分和自定义字符串缓冲区?

javascript - 自定义数字格式函数无法将 0 作为小数返回