将50,000+字符的JSON字符串解析为javascript对象

时间:2022-08-26 11:59:05

I'm trying to evaluate a string of 50,000+ characters from an ajax GET request using jquery. On smaller datasets, the code will evaluate it correctly, but firefox throws an error "Unterminated string literal". After some digging, I tried using external libraries from JSON.org, replacing \n, \r\n, and \r with an empty string (on the server), and encapsulating the eval() with parentheses.

我正在尝试使用jquery对来自ajax GET请求的50,000多个字符进行评估。在较小的数据集上,代码将正确地评估它,但是firefox抛出一个错误“未终止的字符串文字”。在进行了一些挖掘之后,我尝试使用JSON.org的外部库,用一个空字符串(在服务器上)替换\n、\r和\r,并用圆括号封装eval()。

Here is some of the client-side code (javascript):

下面是一些客户端代码(javascript):

http://pastebin.com/wsXuN7tb <- Here I've used an external library to do it

http://pastebin.com/wsXuN7tb <-我在这里使用了一个外部库

After looking through firebug, I noticed that the json string returned by the server was not complete, and was cut off at 50,000 or so characters. I know for a fact the server is returning a valid json string because I dumped it to a file before sending it to the client, but the client ends up receiving a truncated version.

在查看了firebug之后,我注意到服务器返回的json字符串不完整,并且在大约50,000个字符处被截断。我知道服务器正在返回一个有效的json字符串,因为我在将其发送给客户端之前将其转储到一个文件中,但是客户端最终接收到一个被截断的版本。

Why is this happening? Is there any way around this?

为什么会这样?有办法解决这个问题吗?

2 个解决方案

#1


1  

URLs have a length limit that varies from browser to browser. 50,000+ characters is definitely WAY over every browser's limit. For such large data, you should be using a POST instead.

url的长度限制不同于浏览器。5万个字符绝对超过了所有浏览器的限制。对于如此庞大的数据,您应该使用POST。

There is quite literally NOTHING you can do about this limit, as it's a browser limit, and not something you can change on the server. The only thing you can go is switch to using POST.

对于这个限制,您几乎无能为力,因为它是一个浏览器限制,而不是您可以在服务器上更改的东西。你唯一能做的就是改用POST。

#2


0  

Turns out the NetworkStream I used in my c# server could not have a buffer that large, so I just wrote half of the buffer, flushed it, and wrote the other half.

原来我在c#服务器中使用的NetworkStream没有这么大的缓冲区,所以我只写了一半的缓冲区,刷新了它,然后写了另一半。

Thanks for helping guys.

谢谢你的帮助。

#1


1  

URLs have a length limit that varies from browser to browser. 50,000+ characters is definitely WAY over every browser's limit. For such large data, you should be using a POST instead.

url的长度限制不同于浏览器。5万个字符绝对超过了所有浏览器的限制。对于如此庞大的数据,您应该使用POST。

There is quite literally NOTHING you can do about this limit, as it's a browser limit, and not something you can change on the server. The only thing you can go is switch to using POST.

对于这个限制,您几乎无能为力,因为它是一个浏览器限制,而不是您可以在服务器上更改的东西。你唯一能做的就是改用POST。

#2


0  

Turns out the NetworkStream I used in my c# server could not have a buffer that large, so I just wrote half of the buffer, flushed it, and wrote the other half.

原来我在c#服务器中使用的NetworkStream没有这么大的缓冲区,所以我只写了一半的缓冲区,刷新了它,然后写了另一半。

Thanks for helping guys.

谢谢你的帮助。