How can I get ColdFusion methods to return numeric values as strings in JSON format?

别等时光非礼了梦想. 提交于 2020-02-05 08:19:33

问题


ColdFusion 8

This is as simple pseudo-code as there can be:

<cffunction name="badJSON" access="remote" output="true" returntype="string" returnformat="JSON">
    <cfreturn "06762" />
</cffunction>

Results in:

6762.0

It should result in:

06762

Is there a way to get JSON to not convert my value to numeric?

I know I can use WDDX, but that is out of the question.


回答1:


Have you tried this?

return javaCast("string", "06762")




回答2:


Ok, so there is no real way to do this in CF8. The workaround is to add a character to the beginning or end of the string, and then handle it in the client side code. I'm working with USA ZIP Codes, so it was simply a matter of formatting each ZIP as ZIP+4 (00000-0000). That little dash in there makes it a string.




回答3:


I think this is fixed in CF9.0.1 with Cumulative Hotfix 1... which version are you using? If you really want "06762" you can try returntype="plain"



来源:https://stackoverflow.com/questions/3640003/how-can-i-get-coldfusion-methods-to-return-numeric-values-as-strings-in-json-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!