为什么我的数据库中的 null 字符串未使用 returnFormat = JSON 返回
{
MESSAGE = LoginWelcomeBackMessage;
SUCCESS = 1;
USER = {
AcceptAllFriendRequests = 0;
CreateDate = "June, 07 2010 00:00:00";
DayOfBirth = "April, 24 1974 00:00:00";
UserName = "Erik Madsen";
};
}
我的 CFC 定义了数据库表中的所有列,但是当列为 NULL 时,该字段不会作为 JSON 中的属性返回?上面的例子应该有 PersonalInfo = "";有没有办法强制它返回空字符串?
{
MESSAGE = LoginWelcomeBackMessage;
SUCCESS = 1;
USER = {
AcceptAllFriendRequests = 0;
CreateDate = "June, 07 2010 00:00:00";
DayOfBirth = "April, 24 1974 00:00:00";
UserName = "Erik Madsen";
};
}
My CFC defines all the columns in my database table, but when a column is NULL, the field isn't returned as a property in the JSON? The example above should have PersonalInfo = ""; Is there a way to force it to return an empty string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在大多数情况下,简单地忽略该属性可能比空字符串更接近于
null
。如果你想要一个空字符串,你应该返回一个空字符串。如何处理空值取决于您的序列化程序。在 javascript 中,官方实现处理得很好:
Simply leaving the property out is probably closer to
null
than an empty string in most circumstances. if you want an empty string you should return an empty string. how a null value is treated depends on your serializer.In javascript, the official implementation handles it quite fine:
数据库列中的空字符串与列中的 NULL 不同。
看看这篇文章: http://www.bennadel.com/blog/1654-Learning-ColdFusion-9-IsNull-And-Working-With-NULL-Values.htm
通常,您可能希望将它们包装在数据库(在视图或计算列中)使用 ISNULL(col, '') 或 COALESCE(col, '') 或在从数据库中提取后处理这些内容,如本文所述。或者甚至不允许数据库中存在 NULL,而需要空字符串。
An empty string in a database column is not the same as a NULL in a column.
Have a look at this article: http://www.bennadel.com/blog/1654-Learning-ColdFusion-9-IsNull-And-Working-With-NULL-Values.htm
Typically, you may want to wrap these at the database (in a view or a computed column) with ISNULL(col, '') or COALESCE(col, '') or handle these after extraction from the database as the article discusses. Or don't even allow NULLs in the database, require empty strings instead.