使用SerializeJSON返回结构数组,而不是带有COLUMNS和DATA节点的JSON对象? [英] Use SerializeJSON to return an array of structs instead of JSON object with COLUMNS and DATA nodes?

查看:176
本文介绍了使用SerializeJSON返回结构数组,而不是带有COLUMNS和DATA节点的JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Railo应用程序,该应用程序处理通过Ajax来回发送的大量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.

这是一个很好的例子.

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

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",...]}

我编写了一个函数,该函数利用下划线的map()函数将该格式转换为具有命名节点的对象数组:

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;
});
};

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

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"}.
...
]

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

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!

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

推荐答案

eval仅应在少数非常罕见的情况下使用,这当然不是其中一种.停止使用eval并改为执行此操作:

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];

在JavaScript中,foo['bar']等效于foo.bar.

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

这篇关于使用SerializeJSON返回结构数组,而不是带有COLUMNS和DATA节点的JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆