gpt4 book ai didi

coldfusion - 如何防止包含单词 "yes"或 "no"的字符串打印为真或假?

转载 作者:行者123 更新时间:2023-12-04 01:50:51 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How can I prevent SerializeJSON from changing Yes/No/True/False strings to boolean?

(7 个回答)


5年前关闭。




我目前正在设置许多变量,如下所示:

<cfset printPage = "YES">

最终,当我打印出这些变量时,我尝试设置为“YES”的任何内容都会打印为“true”。任何设置为“NO”的内容都会打印为“false”。我不反对使用 YesNoFormat功能。事实上,我最终可能会在此应用程序中使用该函数,但同时我想知道 ColdFusion 是否实际上将单词“YES”和“NO”存储在内存中,或者是否将它们转换为 bool 格式在幕后。

如果 CF 完全按照我声明的方式存储我的变量,我将如何将这些变量作为字符串检索?如果 CF 以某种方式更改变量,是否有任何特殊字符或关键字可以用来强制它将变量存储为字符串?

感谢所有评论/回答的人。我做了更多的实验和阅读,似乎serializeJSON函数会自动将"is"转换为“真”,将“否”转换为“假”。我要么需要在我的 javascript 中处理这个问题,要么我可以在受影响的属性中添加一个空格来规避这种行为。

最佳答案

您已经知道如何将 bool 值显示为"is"或“否”(使用 YesNoFormat())。我认为没有办法强制 ColdFusion 以某种方式存储变量。它只是不支持。我猜你可以通过使用 JavaCast() 直接调用 Java 类型.我只是不明白你为什么要为这样的事情做额外的工作。如果您愿意,您当然可以多研究一下。 Here is a link to the document for JavaCast .

看看这个关于 data types in ColdFusion 的文档.我将在此处发布该文档中的一些相关要点,但请阅读该页面以获取更多信息。

ColdFusion is often referred to as typeless because you do not assign types to variables and ColdFusion does not associate a type with the variable name. However, the data that a variable represents does have a type, and the data type affects how ColdFusion evaluates an expression or function argument. ColdFusion can automatically convert many data types into others when it evaluates expressions. For simple data, such as numbers and strings, the data type is unimportant until the variable is used in an expression or as a function argument.

ColdFusion variable data belongs to one of the following type categories:

Simple One value. Can use directly in ColdFusion expressions. Include numbers, strings, Boolean values, and date-time values.

Binary Raw data, such as the contents of a GIF file or an executable program file.

Complex A container for data. Generally represent more than one value. ColdFusion built-in complex data types include arrays, structures, queries, and XML document objects. You cannot use a complex variable, such as an array, directly in a ColdFusion expression, but you can use simple data type elements of a complex variable in an expression. For example, with a one-dimensional array of numbers called myArray, you cannot use the expression myArray * 5. However, you could use an expression myArray[3] * 5 to multiply the third element in the array by five.

Objects Complex constructs. Often encapsulate both data and functional operations.



它继续说关于数据类型:

Data type notes
Although ColdFusion variables do not have types, it is often convenient to use “variable type” as a shorthand for the type of data that the variable represents.

ColdFusion provides the following functions for identifying the data type of a variable:

  • IsArray
  • IsBinary
  • IsBoolean
  • IsImage
  • IsNumericDate
  • IsObject
  • IsPDFObject
  • IsQuery
  • IsSimpleValue
  • IsStruct
  • IsXmlDoc

ColdFusion also includes the following functions for determining whether a string can be represented as or converted to another data type:

  • IsDate
  • IsNumeric
  • IsXML


所以在你的代码中你可以使用类似 IsBoolean(printPage) 的东西检查它是否包含 bool 值。当然,这并不意味着它实际上被存储为一个 bool 值,而是 ColdFusion 可以将它的值解释为一个 bool 值。

关于coldfusion - 如何防止包含单词 "yes"或 "no"的字符串打印为真或假?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40659510/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com