如何从 ColdFusion 中的 CFC 返回小写 JSON?

发布于 2024-12-04 06:32:15 字数 464 浏览 1 评论 0原文

我有一个 ColdFusion 组件,它将返回一些 JSON 数据:

component
{
    remote function GetPeople() returnformat="json"
    {
        var people = entityLoad("Person");
        return people;
    }
}

不幸的是,返回的 JSON 的所有属性名称均为大写:

[
    {
        FIRSTNAME: "John",
        LASTNAME: "Doe"
    },
    {
        FIRSTNAME: "Jane",
        LASTNAME: "Dover
    }
]

有没有办法强制框架返回 JSON,以便属性名称全部为小写(也许是其他人编写的自定义 UDF/CFC)?

I have a ColdFusion component that will return some JSON data:

component
{
    remote function GetPeople() returnformat="json"
    {
        var people = entityLoad("Person");
        return people;
    }
}

Unfortunately, the returned JSON has all the property names in upper case:

[
    {
        FIRSTNAME: "John",
        LASTNAME: "Doe"
    },
    {
        FIRSTNAME: "Jane",
        LASTNAME: "Dover
    }
]

Is there any way to force the framework to return JSON so that the property names are all lower-case (maybe a custom UDF/CFC that someone else has written)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

并安 2024-12-11 06:32:15

是的,不幸的是,这就是 ColdFusion 的工作方式。设置某些变量时,您可以强制使用小写,就像结构一样:

<cfset structName.varName = "test" />

将设置名称大写的变量。但是:

<cfset structName['varname'] = "test" />

将强制使用小写(或驼峰式,具体取决于您传入的内容)。

但是对于您正在做的 ORM 工作,我认为您无法对其进行任何控制。如果我错了,有人纠正我。

Yeah, unfortunately, that is just the way ColdFusion works. When setting some variables you can force lowercase, like with structs:

<cfset structName.varName = "test" />

Will set a the variable with uppercase names. But:

<cfset structName['varname'] = "test" />

Will force the lowercase (or camelcase depending on what you pass in).

But with the ORM stuff you are doing, I don't think you are going to be able to have any control over it. Someone correct me if I am wrong.

筱果果 2024-12-11 06:32:15

来自 http://livedocs.adobe.com/coldfusion /8/htmldocs/help.html?content=functions_s_03.html
注意:ColdFusion 在内部使用以下方式表示结构键名称
全大写字符,因此将键名称序列化为
全大写的 JSON 表示形式。任何处理 JSON 的 JavaScript
ColdFusion 结构的表示必须使用全大写
结构键名称,例如 CITY 或 STATE。您还可以使用
全大写名称 COLUMNS 和 DATA 作为两个数组的键
表示 JSON 格式的 ColdFusion 查询。

如果你自己定义变量,你可以使用方括号表示法(如 Jason 的答案所示),但对于像 ORM 这样的内置东西,我认为你被困住了 - 除非你想创建自己的结构,并克隆 ORM 版本手动将每个键变成小写,但这并不是一个很好的解决方案。 :/

From http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_s_03.html
Note: ColdFusion internally represents structure key names using
all-uppercase characters, and, therefore, serializes the key names to
all-uppercase JSON representations. Any JavaScript that handles JSON
representations of ColdFusion structures must use all-uppercase
structure key names, such as CITY or STATE. You also use the
all-uppercase names COLUMNS and DATA as the keys for the two arrays
that represent ColdFusion queries in JSON format.

If you're defining the variables yourself, you can use bracket notation (as Jason's answer shows), but with built-in stuff like ORM I think you're stuck - unless you want to create your own struct, and clone the ORM version manually, lower-casing each of the keys, but that's not really a great solution. :/

生生不灭 2024-12-11 06:32:15

这应该像你所描述的那样工作。

component
{
    remote function GetPeople() returnformat="json"
    {
        var people = entityLoad("Person");
        var rtn = [];
        for ( var i = 1; i <= arrayLen( people ); i++ ) {
            arrayAppend( rtn, {
                "firstname" = people[i].getFirstname(),
                "lastname" = people[i].getLastname()
            } );
        }
        return rtn;
    }
}

如果任何实体属性返回 null,则结构键将不存在。
要解决这个问题,请尝试这个

component
{
    remote function GetPeople() returnformat="json"
    {
        var people = entityLoad("Person");
        var rtn = [];
        for ( var i = 1; i <= arrayLen( people ); i++ ) {
            var i_person = {
                "firstname" = people[i].getFirstname(),
                "lastname" = people[i].getLastname()
            };
            if ( !structKeyExists( i_person, "firstname" ) ) {
                i_person["firstname"] = ""; // your default value
            }
            if ( !structKeyExists( i_person, "lastname" ) ) {
                i_person["lastname"] = ""; // your default value
            }
            arrayAppend( rtn, i_person );
        }
        return rtn;
    }
}

This should work as you described.

component
{
    remote function GetPeople() returnformat="json"
    {
        var people = entityLoad("Person");
        var rtn = [];
        for ( var i = 1; i <= arrayLen( people ); i++ ) {
            arrayAppend( rtn, {
                "firstname" = people[i].getFirstname(),
                "lastname" = people[i].getLastname()
            } );
        }
        return rtn;
    }
}

If any of your entity properties return null, the struct key wont exist.
To work around that try this

component
{
    remote function GetPeople() returnformat="json"
    {
        var people = entityLoad("Person");
        var rtn = [];
        for ( var i = 1; i <= arrayLen( people ); i++ ) {
            var i_person = {
                "firstname" = people[i].getFirstname(),
                "lastname" = people[i].getLastname()
            };
            if ( !structKeyExists( i_person, "firstname" ) ) {
                i_person["firstname"] = ""; // your default value
            }
            if ( !structKeyExists( i_person, "lastname" ) ) {
                i_person["lastname"] = ""; // your default value
            }
            arrayAppend( rtn, i_person );
        }
        return rtn;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文