防止ColdFusion使用serizejson将字符串转换为数字

时间:2022-05-19 07:32:59

I have ColdFusion 9.0.1 with the latest hotfix (4). I need ColdFusion to return all JSON data with quotes around them (as strings). I have the following problem:

我有ColdFusion 9.0.1和最新的hotfix(4),我需要ColdFusion返回所有带引号的JSON数据(作为字符串)。我有以下问题:

<cfset test = StructNew()>
<cfset test.name = "1234.100">
<cfoutput>#SerializeJSON(test)#</cfoutput>

The text that is outputted is:

输出的文本为:

{"name":1234.100}

Every javascript JSON parser converts that to 1234.1 and is not keeping the trailing 0's. I either need ColdFusion to output as string or a javascript parser to keep the trailing 0's. Any ideas?

每个javascript JSON解析器都将其转换为1234.1,并没有保留后面的0。我要么需要ColdFusion作为字符串输出,要么需要一个javascript解析器来保持后面的0。什么好主意吗?

This is a simplified example. I am grabbing this data from a database.

这是一个简化的例子。我正在从数据库中获取这些数据。

4 个解决方案

#1


4  

Here's a solution - albeit a very hacky, inelegant solution...

这里有一个解决方案——尽管是一个非常陈腐、不雅的方案……

Your setup:

你的设置:

var test = {
  name = "1234.100"
};

Adding some obvious string to the front forces the value to become a string when it is converted to JSON. Then we get rid of this ugly string.

向前端添加一些明显的字符串将使值在转换为JSON时变成字符串。然后我们去掉这条难看的线。

var thisIsSuchAHorribleHack = "(!@$!@$)";
test.name = thisIsSuchAHorribleHack & test.name;
var serializedTest = SerializeJSON(test);
serializedTest = Replace(serializedTest, thisIsSuchAHorribleHack, "", "ALL");
writeOutput(serializedTest);

#2


0  

We've had a lot of luck using Jackson to get round the nightmare that is CF json handling.

我们很幸运地使用Jackson来解决CF json处理的噩梦。

#3


0  

Just add a simple whitespace at the beginning of your number. I tried doing it at the end but it doesn't work.

在数字的开头添加一个简单的空格即可。我试着在最后做,但没用。

<cfset test = StructNew()>
<cfset test.name = " 1234.100">
<cfoutput>#SerializeJSON(test)#</cfoutput>

The output will be

输出将

{"name":" 1234.100"}

#4


0  

If you don't want to use a kludge, you can use a third party library that encodes JSON correctly. I used JSONUtil from http://jsonutil.riaforge.org/. I am using ColdFusion 9 so I don't know if the more recent versions of ColdFusion have fixed some of the encoding abnormalities.

如果您不想使用拼凑器,您可以使用一个第三方库来正确编码JSON。我使用了来自http://jsonutil.riaforge.org/的JSONUtil。我正在使用ColdFusion 9,所以我不知道最近版本的ColdFusion是否修复了一些编码异常。

#1


4  

Here's a solution - albeit a very hacky, inelegant solution...

这里有一个解决方案——尽管是一个非常陈腐、不雅的方案……

Your setup:

你的设置:

var test = {
  name = "1234.100"
};

Adding some obvious string to the front forces the value to become a string when it is converted to JSON. Then we get rid of this ugly string.

向前端添加一些明显的字符串将使值在转换为JSON时变成字符串。然后我们去掉这条难看的线。

var thisIsSuchAHorribleHack = "(!@$!@$)";
test.name = thisIsSuchAHorribleHack & test.name;
var serializedTest = SerializeJSON(test);
serializedTest = Replace(serializedTest, thisIsSuchAHorribleHack, "", "ALL");
writeOutput(serializedTest);

#2


0  

We've had a lot of luck using Jackson to get round the nightmare that is CF json handling.

我们很幸运地使用Jackson来解决CF json处理的噩梦。

#3


0  

Just add a simple whitespace at the beginning of your number. I tried doing it at the end but it doesn't work.

在数字的开头添加一个简单的空格即可。我试着在最后做,但没用。

<cfset test = StructNew()>
<cfset test.name = " 1234.100">
<cfoutput>#SerializeJSON(test)#</cfoutput>

The output will be

输出将

{"name":" 1234.100"}

#4


0  

If you don't want to use a kludge, you can use a third party library that encodes JSON correctly. I used JSONUtil from http://jsonutil.riaforge.org/. I am using ColdFusion 9 so I don't know if the more recent versions of ColdFusion have fixed some of the encoding abnormalities.

如果您不想使用拼凑器,您可以使用一个第三方库来正确编码JSON。我使用了来自http://jsonutil.riaforge.org/的JSONUtil。我正在使用ColdFusion 9,所以我不知道最近版本的ColdFusion是否修复了一些编码异常。