2

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 why does the .0 get added? and is there some way to remove it?

erikvold
  • 15,988
  • 11
  • 54
  • 98
  • There are couple of questions discussing this already. This http://stackoverflow.com/q/3640003/56604 or this http://stackoverflow.com/q/2479737/56604 / there are few workarounds proposed, select which you like more. – Sergey Galashyn Jan 18 '11 at 18:24

4 Answers4

5

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

struct.x = javaCast("int",1);
Matthieu
  • 4,605
  • 4
  • 40
  • 60
Olivier
  • 51
  • 1
  • 1
4

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"}
Saul
  • 1,387
  • 5
  • 23
  • 45
0

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

Balázs Németh
  • 6,222
  • 9
  • 45
  • 60
-1

Two ways to remove it:

NumberFormat(x, "9")

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

duncan
  • 31,401
  • 13
  • 78
  • 99
  • both methods don't do what I want. – erikvold Jan 20 '11 at 18:42
  • ah, they would both work on the values in the JSON struct AFTER the serialization, but not if you applied them to the numbers in your original struct. I guess that's what you really wanted, as you might be passing the JSON to javascript or whatever which wouldn't have access to these CFML methods. – duncan Jan 21 '11 at 12:00