NSData 中的 NSString -- 服务器返回带有前导 null 的 NSData

发布于 2024-12-04 21:16:22 字数 564 浏览 2 评论 0原文

我还没有看到关于这个问题的非常明确的答案。由于某种我不明白的原因,我接收数据的服务器返回一个 JSON 响应,开头有很多(可能是数百个)空字节。

当我使用以下代码时,字符串似乎为空:

    NSString* newStr = [[[NSString alloc] initWithData:dataToBeLoaded encoding:NSUTF8StringEncoding] autorelease];

这显然是一个常见问题,但我还没有看到关于如何将 NSData 转换为 NSString 的明确答案。

直到最近,我在中间 NSString 值上看到了这个问题,但在我从开头和结尾删除了几个(但不是数百个)字符后,这个问题就消失了。剥离后的 NSString 完全没问题,所以我认为编码方法不是问题。

是否有内置方法可以正确执行此操作?我必须想象,我编写的逐字节遍历的代码会更有效。

顺便说一句,当我在浏览器中手动将 JSON 请求发送到服务器时,响应会以完全有效的 JSON 形式返回,浏览器屏幕上不会出现明显的问题。

有什么想法我应该做什么吗?谢谢。

I haven't seen a very clear answer on this one. For some reason I don't understand, the server I am receiving data from returns a JSON response with a lot (hundreds, maybe) of null bytes at the beginning.

When I use the following code, the string appears to be null:

    NSString* newStr = [[[NSString alloc] initWithData:dataToBeLoaded encoding:NSUTF8StringEncoding] autorelease];

This is apparently a common problem, but I haven't seen a clear answer on how to convert the NSData to an NSString.

Up until recently, I saw this problem with an intermediate NSString value, but it went away after I stripped a handful (but not hundreds) of characters off of the beginning and end. The NSString after stripping was perfectly fine, so I suppose the encoding method was not the problem.

Is there a built-in method that will do this properly? I have to imaging it would be more efficient that code I would write to go through byte-by-byte.

By the way, when I manually send the JSON request to the server in my browser, the response comes back as perfectly valid JSON, with no obvious problems showing up in the browser screen.

Any ideas what I should do? Thanks.

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

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

发布评论

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

评论(1

岛歌少女 2024-12-11 21:16:22

从每个服务器返回的数据包含有效的 JSON(在许多情况下使用 JSON Lint 进行验证)。在一种情况下,返回数据是包含 JSON 对象的 JavaScript 赋值语句。在另一种情况下,它只是一个 JSON 对象。在这两种情况下,我都知道 JSON 对象以大括号开头。

因此,为了去掉所有前导空值,我使用下面的代码。似乎某些方法将 NSString 中的字符串数据视为 C 字符串,但其他方法知道它还有更多内容,包括长度。幸运的是,rangeOfStringsubstringFromIndex 超越了前导空值。

NSString* newStr = [[[NSString alloc] initWithData:dataToBeLoaded encoding:NSUTF8StringEncoding] autorelease];

if (newStr == nil) return;

NSRange headRange = [newStr rangeOfString:@"{"];
NSString *stripped = [newStr substringFromIndex:headRange.location];

The data that is being returned from each server contains valid JSON (verified with JSON Lint in many instances). In one case, the return data is a javascript assignment statement containing a JSON object. In another case, it's simply a JSON object. In both cases, I know that the JSON object begins with a curly bracket.

So, to strip off all of the leading nulls, I use the code below. It seems that some methods treat the string data in an NSString like a C string, but other methods are aware that there is more to it, including an awareness of the length. Fortunately, rangeOfString and substringFromIndex look beyond the leading nulls.

NSString* newStr = [[[NSString alloc] initWithData:dataToBeLoaded encoding:NSUTF8StringEncoding] autorelease];

if (newStr == nil) return;

NSRange headRange = [newStr rangeOfString:@"{"];
NSString *stripped = [newStr substringFromIndex:headRange.location];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文