Stig JSON 库解析错误:如何在 JSON 中容纳新行?

发布于 2024-12-06 01:35:09 字数 796 浏览 1 评论 0原文

我有一些从 Web 服务返回的 xml。我反过来使用 xslt 将该 xml 转换为 json (我正在将其他人的 xml 服务转换为基于 json 的服务)。我的服务现在正在输出 JSON,由我的 iphone 应用程序使用事实上的 iphone json 框架 SBJSON 来使用。

问题是,使用 [string JSONValue] 方法会阻塞,我可以看到这是由于换行符造成的。你瞧,即使常见问题解答也告诉了我问题,但我不知道如何解决它。

解析器无法解析字符串 X

您确定它是合法的 JSON 吗?这个框架非常严格,所以不会接受(显然)几个验证器接受的东西。特别是,字符串标记中不允许使用文字 TAB、NEWLINE 或 CARRIAGE RETURN(以及所有其他控制字符)字符,但可能很难发现。 (当然,这些字符在标记之间是允许的。)

如果您得到类似下面的内容(数字可能会有所不同),则您的字符串之一不允许使用 Unicode 控制字符。

NSLocalizedDescription = "未转义的控制字符 '0x9'";

我尝试使用以下行: NSString *myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n "];

但这不起作用。我的 xml 服务不会以 CDATA 的形式返回。据我所知,xml 中确实有一个换行符(我如何确认这一点)。我只想将换行符忠实地传输为 JSON。

我实际上花了一整天的时间在这上面,所以是时候问一下了。我已经没有骄傲了。 多谢

I have some xml that is coming back from a web service. I in turn use xslt to turn that xml into json (I am turning someone else's xml service into a json-based service). My service, which is now outputting JSON, is consumed by my iphone app using the de facto iphone json framework, SBJSON.

The problem is, using the [string JSONValue] method chokes, and I can see that it's due to line breaks. Lo and behold, even the FAQ tells me the problem but I don't know how to fix it.

The parser fails to parse string X

Are you sure it's legal JSON? This framework is really strict, so won't accept stuff that (apparently) several validators accepts. In particular, literal TAB, NEWLINE or CARRIAGE RETURN (and all other control characters) characters in string tokens are disallowed, but can be very difficult to spot. (These characters are allowed between tokens, of course.)

If you get something like the below (the number may vary) then one of your strings has disallowed Unicode control characters in it.

NSLocalizedDescription = "Unescaped control character '0x9'";

I have tried using a line such as: NSString *myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];

But that doesn't work. My xml service is not coming back as CDATA. The xml does have a line break in it as far as I can tell (how would I confirm this). I just want to faithfully transmit the line break into JSON.

I have actually spent an entire day on this, so it's time to ask. I have no pride anymore.
Thanks alot

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

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

发布评论

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

评论(3

昔梦 2024-12-13 01:35:09

转义新行字符应该可以。因此,理想情况下,以下行应该有效。只需检查您的输入是否还包含 '\r' 字符。

 NSString *myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];

您可以使用任何支持显示所有字符(也包括不可显示字符)的编辑器检查字符串中存在哪个控制字符。例如,使用 Notepad++ 您可以查看字符串中包含的所有字符。

Escaping a new line character should work. So following line should ideally work. Just check if your input also contains '\r' character.

 NSString *myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];

You can check which control character is present in the string using any editor which supports displaying all characters (non-displayable characters as well). e.g. using Notepad++ you can view all characters contained in a string.

梦言归人 2024-12-13 01:35:09

听起来您的 XSLT 不起作用,因为它没有生成合法的 JSON。这并不奇怪,因为生成格式正确的 JSON 字符串并不完全是小事。我想知道仅使用标准 XML 库将 XML 解析为应用程序可以使用的数据结构是否会更简单。

It sounds like your XSLT is not working, in that it is not producing legal JSON. This is unsurprising, as producing correctly formatted JSON strings is not entirely trivial. I'm wondering if it would be simpler to just use the standard XML library to parse the XML into data structures that your app can consume.

三生殊途 2024-12-13 01:35:09

我没有适合你的解决方案,但我通常使用 TouchJSON 项目中的 CJSONSerializer 和 CJSONDeserializer,它非常可靠,我以前从未遇到过换行问题。只是一个想法。

http://code.google.com/p/touchcode/source/browse/TouchJSON/Source/JSON/CJSONDeserializer.m?r=6294fcb084a8f174e243a68ccfb7e2c519def219
http://code.google.com/p/touchcode/source/browse/TouchJSON/Source/JSON/CJSONSerializer.m?r=3f52118ae2ff60cc34e31dd36d92610c9dd6c306

I don't have a solution for you, but I usually use CJSONSerializer and CJSONDeserializer from the TouchJSON project and it is pretty reliable, I have never had a problem with line breaks before. Just a thought.

http://code.google.com/p/touchcode/source/browse/TouchJSON/Source/JSON/CJSONDeserializer.m?r=6294fcb084a8f174e243a68ccfb7e2c519def219
http://code.google.com/p/touchcode/source/browse/TouchJSON/Source/JSON/CJSONSerializer.m?r=3f52118ae2ff60cc34e31dd36d92610c9dd6c306

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