开发者

Why does SerializeJSON return an int with .0?

开发者 https://www.devze.com 2023-02-05 22:09 出处:网络
The following code: <cfscript> struct = StructNew(); struct.x = 1; struct.y = 2; </cfscript> <cfoutput>#SerializeJSON(struct)#</cfoutput>

The following code:

<cfscript>
struct = StructNew();
struct.x = 1;
struct.y = 2;
</cfscript>

<cfoutput>#SerializeJSON(struct)#</cfoutput>

Results in the following output:

{"Y":2.0,"X":1.0}

So my question is wh开发者_StackOverflowy does the .0 get added? and is there some way to remove it?


The best thing I found to remove the .0 is a cast, using the Coldfusion function javaCast :

struct.x = javaCast("int",1);


Adding a trailing .0 to numbers was a known "feature" in the way serializeJson was implemented in release 8. It is fixed/changed in CF 9.01 on my home XP machine your code returns

{"Y":"2","X":"1"}


i assume the implementation is not the best :-/ try to checkout this: http://craigkaminsky.blogspot.com/2008/11/coldfusion-serializejson-gotcha.html


Two ways to remove it:

NumberFormat(x, "9")

<cfset x = 1.0> <cfset y = x * 1> <!--- will convert to an int --->

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号