有没有办法通过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>
结果
关于json - 返回 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56599523/