使用 SerializeJSON 返回结构数组而不是带有 COLUMNS 和 DATA 节点的 JSON 对象?

发布于 2024-09-12 09:30:40 字数 1175 浏览 6 评论 0原文

我正在构建一个 Railo 应用程序,它处理通过 Ajax 来回发送的大量 JSON 数据。我已经找到了优化其性能的机会,但在解决这个问题之前我想听听社区的一些建议。

这是一个很好的例子来说明这种情况。

我在服务器上有一个操作,用于查询一组出价响应,将它们序列化为 JSON,然后将它们返回到前端的 javascript,然后解析并呈现一些 HTML。 Railo 返回 JSON 的格式是熟悉的两节点对象:

{"COLUMNS":["one","two","three",...],"DATA":["value","value","value",...]}

我编写了一个使用 underscore 的 map() 函数将此格式转换为具有命名节点的对象数组:

function toArgsObject(data,columns) {
return _.map(data, function(w){
    var q = {};
    for (var i=0; i < w.length; i++) { eval("q."+columns[i]+" = w[i]"); };
    return q;
});
};

这很好地完成了工作,但是性能不是很好!即使使用像这样的 swift js 解释器在 webkit 和 firefox 中,该函数通常占调用它的函数中 75% 的处理时间,尤其是当数据集很大时。我想看看通过将此处理卸载到服务器可以得到多少改进,但我不太有 cfml / cfscript 能力来编写此的有效版本。

那么,我需要从服务器返回的内容将如下所示:

[
{"one":"value","two":"value","three":"value"},
{"one":"value","two":"value","three":"value"}.
...
]

我知道 SerializeJSON 使用的格式创建的响应要小得多,因此使用更少的带宽来发送。这就是实验的用武之地。我想看看它如何影响我的应用程序以不同的方式做事!

有人编写过可以返回这种格式数据的 JSON 序列化程序吗?

I am building a Railo app which deals with a lot of JSON data sent back and forth via Ajax. I've identified an opportunity to optimize its performance, but I'd like to hear some advice from the community before I tackle it.

Here is a good example of the situation.

I have an action on the server that queries a set of bid responses, serializes them to JSON, then returns them to my javascript on the front end, which then parses and renders some HTML. The format in which Railo returns the JSON is the familiar two-node object:

{"COLUMNS":["one","two","three",...],"DATA":["value","value","value",...]}

I wrote a function that utilizes underscore's map() function to convert this format into an array of objects with named nodes:

function toArgsObject(data,columns) {
return _.map(data, function(w){
    var q = {};
    for (var i=0; i < w.length; i++) { eval("q."+columns[i]+" = w[i]"); };
    return q;
});
};

This gets the job done nicely, however the performance isn't very good! Even with swift js interpreters like those in webkit and firefox, this function often accounts for 75% of processing time in the functions that call it, especially when the data sets are large. I'd like to see how much improvement I would get by offloading this processing to the server, but I don't quite have the cfml / cfscript chops to write an efficient version of this.

What I need to come back from the server, then, would look like this:

[
{"one":"value","two":"value","three":"value"},
{"one":"value","two":"value","three":"value"}.
...
]

I understand that the format used by SerializeJSON creates responses that are far smaller and therefore use less bandwidth to send. This is where the experimentation comes in. I'd like to see how it impacts my application to do things differently!

has anyone written a JSON Serializer that can return data in this format?

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

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

发布评论

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

评论(3

对你的占有欲 2024-09-19 09:30:40

如果您需要 JS 中的结构数组,您可以轻松地将查询转换为服务器端的此类数据集并应用 SerializeJSON()。

快速示例 1

<cfset dataset = [
    {"one":"value","two":"value","three":"value"},
    {"one":"value","two":"value","three":"value"}
] />

<cfoutput><p>#SerializeJSON(dataset)#</p></cfoutput>

(我喜欢 Railo 中结构定义语法的自由)

快速示例 2

<cfquery datasource="xxx" name="qGetRecords">
    select userId, login, email from users limit 0,3
</cfquery>

<cfset dataset = [] />

<cfloop query="qGetRecords">
    <cfset record = {} />
    <cfset record["one"] = qGetRecords.userId />
    <cfset record["two"] = qGetRecords.login />
    <cfset record["three"] = qGetRecords.email />
    <cfset ArrayAppend(dataset, record) />
</cfloop>

<cfoutput>
    <p>#SerializeJSON(qGetRecords)#</p>
    <p>#SerializeJSON(dataset)#</p>
</cfoutput>

应该适合您。

If you need an array of structures in JS, you can easily convert the query into this type of dataset on the server-side and apply the SerializeJSON().

Quick example 1

<cfset dataset = [
    {"one":"value","two":"value","three":"value"},
    {"one":"value","two":"value","three":"value"}
] />

<cfoutput><p>#SerializeJSON(dataset)#</p></cfoutput>

(I love the freedom of structure definition syntax in Railo)

Quick example 2

<cfquery datasource="xxx" name="qGetRecords">
    select userId, login, email from users limit 0,3
</cfquery>

<cfset dataset = [] />

<cfloop query="qGetRecords">
    <cfset record = {} />
    <cfset record["one"] = qGetRecords.userId />
    <cfset record["two"] = qGetRecords.login />
    <cfset record["three"] = qGetRecords.email />
    <cfset ArrayAppend(dataset, record) />
</cfloop>

<cfoutput>
    <p>#SerializeJSON(qGetRecords)#</p>
    <p>#SerializeJSON(dataset)#</p>
</cfoutput>

Should work for you.

在巴黎塔顶看东京樱花 2024-09-19 09:30:40

eval 应该只在极少数情况下使用,这当然不是其中之一。停止使用 eval 并执行此操作:

q[columns[i]] = w[i];

在 JavaScript 中,foo['bar'] 相当于 foo.bar

eval should only be used in a few very rare cases and this certainly isn't one of them. Stop using eval and do this instead:

q[columns[i]] = w[i];

In JavaScript, foo['bar'] is the equivalent of foo.bar.

木槿暧夏七纪年 2024-09-19 09:30:40

在 Coldfusion 10 或 Railo 4 中,您可以使用 toArray() 函数 ="http://russplaysguitar.github.com/UnderscoreCF/" rel="nofollow">Underscore.cfc 库 在调用 serializeJSON() 之前将您的查询转换为您想要的格式。 cfscript 中的示例:

exampleQuery = queryNew("one,two,three","Varchar,Varchar,Varchar",
[
    ["value","value","value"],
    ["value","value","value"]
]);

arrayOfStructs = _.toArray(exampleQuery);

serializeJSON(arrayOfStructs);

结果:

[{ONE:"value", TWO:"value", THREE:"value"}, {ONE:"value", TWO:"value", THREE:"value"}]

toArray() 函数返回与您的查询匹配的结构数组,这就是生成的 JSON 按照您想要的方式格式化的原因。

[免责声明:我写了Underscore.cfc]

In Coldfusion 10 or Railo 4, you could use the toArray() function of the Underscore.cfc library to convert your query into the format you want before calling serializeJSON(). Example in cfscript:

exampleQuery = queryNew("one,two,three","Varchar,Varchar,Varchar",
[
    ["value","value","value"],
    ["value","value","value"]
]);

arrayOfStructs = _.toArray(exampleQuery);

serializeJSON(arrayOfStructs);

Result:

[{ONE:"value", TWO:"value", THREE:"value"}, {ONE:"value", TWO:"value", THREE:"value"}]

The toArray() function returns an array of structs that matches your query, which is why the resulting JSON is formatted in the way that you want.

[Disclaimer: I wrote Underscore.cfc]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文