关于 Android 上的 JSONArray 异常(以及字符串的最大大小)
我正在开发一个 Android (2.2) 应用程序,在该应用程序中,我从 Web 服务获取一个包含自定义对象列表的 JSON 序列化版本的字符串,这会生成 JSONObject 的 JSONArray 字符串。接收到的字符串格式良好,我可以用肉眼看到,也可以用在线验证器证明。在 Android 应用程序中的某个时刻,我将此字符串传递给 JSONArray 构造函数,以获取 JSONObject 数组。问题是这样的:JSONArray 构造函数抛出一个异常,指出字符串在某个时刻以无效终止符结尾:但异常中指示的点距离字符串的真正结尾很远。事实是,所讨论的字符串很大(大约 160000 个字符):有谁知道传递给 JSONArray 构造函数时字符串的长度是否有限制?内存中保存的字符串变量是全长的,所以在我看来 JSONArray 构造函数有点截断它。是否可以?
提前致谢, 问候
I'm developing an Android (2.2) application in which I get from a web service a string containing the JSON serialized version of a list of custom objects, which results in a JSONArray string of JSONObject. The received string is well formed, as I can see at eyesight and as I can prove with online validator. At one point in the Android application, I pass this string to a JSONArray constructor, to get an array of JSONObject. Here's the problem: the JSONArray constructor throws an exception, stating that at some point the string ends with a invalid terminator: but the point indicated in the exception is far from the real end of the string. The fact is that the string in question is huge (about 160000 chars): does anyone know if there's a limit to the length of a string when passed to the JSONArray constructor? The string variable held in memory is full-length, so it seems to me that the JSONArray constructor kindof truncates it. Is it possible?
Thanks in advance,
regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试删除字符串末尾的空格(如果有)并再次解析。
对字符串使用 trim() 并检查一次。
Try removing the spaces at the end of the string if any and parse again.
Use trim() on string and check once.
好吧,伙计们,这都是我的错。我从 http 流中读取了字符串,但我没有意识到有时我会在字符串未完成时获取该字符串(因此我认为这是 JSONArray 错误的异常)。
Ok guys, it's all my fault. I read the string from an http stream and I did not realize that sometimes I get the string when it's not completed (hence the exception which I tought to be be JSONArray fault).