0
votes

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.

1
Why is this supposed to be a common problem? Where else have you seen it? Have you checked with fiddler (or a similar tool) that the JSON response from the server is okay? What's the content of dataToBeLoaded? A complete JSON response or just a part of the JSON response? Please provide more information.Codo
After reading this question again, it seems clear to me that the server part needs to be fixed and not the iOS or Mac OS client? Are you able to fix the server or is it somebody else's responsibility? What server technology is used anyway?Codo
Ok, I don't know if it's common in a world of millions of programmers, but I have seen it a few times with no real answer. The server is not under my control, and I have seen this from two servers I am working with. I'll answer my question below with the solution I found useful for my application.Jim
I can hardly believe this. Can you provide more information about the server (what technology does it use, it is public), a network trace that shows the transmitted data, the code you use to access the server and process the answer etc.?Codo
Sorry. I can't provide the server info. But it's not a question about JSON per se, anyway. It's a question about NSString handling.Jim

1 Answers

0
votes

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];