在Java中的json响应中创建javascript数组

发布于 2024-10-31 12:16:48 字数 654 浏览 2 评论 0原文

我有一个 servlet,我在其中发送 JSON 响应(返回 javascript)。例如,我的响应看起来像

{
    "responseStr":"1,5,119.8406677,7,7,116.5664291,10,10,116.6099319,20,10,117.2185898,25,3,115.2636185"
}

现在正在发生的事情是,我正在 servlet 中收集数据(上面的数字),并将其作为带有逗号分隔值的字符串以 JSON 响应形式发送。当此响应到达前端时,所有这些数字都必须放入 JavaScript 数组中(我在其中执行进一步的逻辑)。目前我正在这样做,

var res = JSON.parse(REQ.responseText);         
var myArr = res.responseStr.split(',');

我的想法是第二行(我使用 split() 的地方)导致我的应用程序出现瓶颈。如上例所示,一些数据点并不是问题,但当我有数千个数据点时,它就成为问题了。

所以我的问题是,有没有一种方法,当我在 servlet 中创建响应时,我可以将响应创建为 javascript 数组,这样我就不必使用 split() 了? 有更好的方法来实现上述将响应转换为 JavaScript 数组的任务吗?

I have a servlet where I send JSON response (back to javascript) . For example , my response looks like

{
    "responseStr":"1,5,119.8406677,7,7,116.5664291,10,10,116.6099319,20,10,117.2185898,25,3,115.2636185"
}

Now what is happening at the moment is that I am collecting data( numbers above) in servlet and sending it in JSON response as a String with comma separated values. When this response reaches front end, all these numbers have to go in a javascript array (where I do my further logic). Currently I am doing this by

var res = JSON.parse(REQ.responseText);         
var myArr = res.responseStr.split(',');

My thinking is that the second line( where I use split()) is causing a bottleneck in my application . A few data points as in above example are not a trouble but it becomes a problem when i have thousands of data points.

So my question is that is there a way that when I am creating my response in servlet that I can create the response as javascript array so that I do not have to use split() at all?
Any better ways of achieving the above task of converting the response into javascript array?

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

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

发布评论

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

评论(3

苄①跕圉湢 2024-11-07 12:16:48

如果您将 responseStr 作为数组发送,那么当 JSON 解析它时,它将是一个数组。因此,您可以将 JSON 响应发送为“[1,2,3,4,5,6,7]”等,当您 JSON.parse 时,它​​将返回一个数组。

为了更清楚一点:

var arr = [1,2,3,4,5];
arr = JSON.stringify(arr); // "[1,2,3,4,5]" -- String
arr = JSON.parse(arr); // [1,2,3,4,5] -- Array

If you send responseStr as an Array, when the JSON parses it, it will be an array. So you could send your JSON response as "[1,2,3,4,5,6,7]" and so one, and when you JSON.parse, it will return an array.

To make it a little more clear :

var arr = [1,2,3,4,5];
arr = JSON.stringify(arr); // "[1,2,3,4,5]" -- String
arr = JSON.parse(arr); // [1,2,3,4,5] -- Array
梦在夏天 2024-11-07 12:16:48

在您的响应中设置内容类型 JSON/application 并发送 JSON 数组

{
    "responseStr":["1","5","119.8406677","7","7","116.5664291","10","10","116.6099319","20","10","117.2185898","25","3","115.2636185"]
}

然后在您的 JavaScript 中您可以简单地使用 (参考):

var myArray = responseJSONObject.responseStr;

您可以使用 JSON.js 来完成各种任务。

In your response set content-type JSON/application and send JSON array

{
    "responseStr":["1","5","119.8406677","7","7","116.5664291","10","10","116.6099319","20","10","117.2185898","25","3","115.2636185"]
}

Then in your JavaScript you can simply use (reference):

var myArray = responseJSONObject.responseStr;

You may utilize JSON.js for various tasks.

三月梨花 2024-11-07 12:16:48

这是一个很好的问题。 JSON 可以简单地返回一个数组

{ "responseStr": [[1], [2], [3], [4] }

酷!

双引号不是必需的,除非您希望它们作为字符串。

另一件事,你也可以拥有多维数组!

{ "responseStr": [[1,10], [2,20], [3,30], [4,40]] }

这个链接是一个很好的参考:

http://json.org/fatfree.html

That is a great question. JSON can return an array as simply as

{ "responseStr": [[1], [2], [3], [4] }

Cool!

Double Quotes are not necessary unless you want them as strings.

One more thing, you can have multi dimensional arrays too!

{ "responseStr": [[1,10], [2,20], [3,30], [4,40]] }

This link is a great reference:

http://json.org/fatfree.html

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