json - 返回一个 JSON 对象
问题描述
有没有办法通过 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>
结果是
推荐阅读
- python - 在 Homebrew 上的 python 2 支持结束后,在 Mac 上安装 python@2
- java - 在 Eclipse 中运行 Spring Boot 应用程序时出现带有分段错误的未处理异常
- c - 我可以在 C 中使用指针作为获取的参数吗?
- html - Bootsrap 固定导航栏没有边距底部
- vuejs2 - 如何清空 Vuex 存储模块状态对象?
- flutter - onBackgroundMessage 打开指定页面
- java - 如何在Java中将字符串表示为二叉树?
- visual-studio-code - 如何使用gnu格式样式格式化vscode中的c代码?
- r - R中的CompareGroup函数_组数大于5时如何编写r代码
- wordpress - WordPress - 使用参数设置自定义帖子类型默认类别(分类术语)