NSJSONSerialization Fails to Parse Valid JSON - "Garbage at End" NSJSONSerialization Fails to Parse Valid JSON - "Garbage at End" json json

NSJSONSerialization Fails to Parse Valid JSON - "Garbage at End"


Strangely, this is what it took! At the start of the didReceiveNetworkData function:

//Yes, I know this wastefully allocates RAM, so I'll fix it later.NSString* string = [[[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] stringByReplacingOccurrencesOfString:@"\t" withString:@""] stringByReplacingOccurrencesOfString:@"\0" withString:@""];data = [string dataUsingEncoding:NSUTF8StringEncoding];

I removed all '\t' (before entries) and '\0' (at the end of string) characters. Removing just '\0' xor '\t' didn't work; it had to be both. Unless I'm missing something, it seems very strange that NSJSONSerialization cares both about tab characters and trailing null characters!

By the way, cJSON encoded this, so that means that cJSON and NSJSONSerialization do not strictly agree. The online JSON tester agrees with the way cJSON encoded it too.

I might be wrong anyway because this was working before without GCDAsyncSocket (though I still had to remove null characters, as I now recall). Or maybe my old solution was somehow removing the tab characters.


Update:You are probably having a problem somehow with the escape characters. I am no JSON guru myself, but I remember that I once had a JSON string added into an dictionary and then created a JSON from that dictionary. The JSON string itself became escapes, so it would stay a string during parsing. My feeling is you need to remove the escape characters.

Maybe you have some race condition and are writing to the data while the JSONParsing is ongoing. Make sure you are passing NSData and not NSMutableData to the method that does the serialization to make sure.Remember that NSJSONSerialization is not a streaming JSON parser. It takes an immutable JSON object and serializes that into a Foundation object. If you are adding bytes to the NSData, NSJSONSerialization will see at some point stream of bytes that at that point is not valid JSON (yet).


I also suffered from this problem even though I was requesting the same JSON file twice. I solved it by changing how my iOS app made web requests by a custom retrieve JSON from URL class made by me:

My old way:

//init the download JSON classvar downloadJSON = createDownloadJSONObject()//Downloads and parses the file correctlydownloadJSON.downloadJSON(url: "http://www.example.com/jsonfile.json")/** waits for the first JSON object to return the json file successfully *///throws the garbage collection error even though requested file is the samedownloadJSON.downloadJSON(url: "http://www.example.com/jsonfile.json")

I can only imagine that the problem came from how I dealt with the cleanup of the objects.

Working Method:

//init the download JSON classvar downloadJSON = createDownloadJSONObject()//Downloads and parses the file correctlydownloadJSON.downloadJSON(url: "http://www.example.com/jsonfile.json")/** waits for the first JSON object to return the json file successfully *///re instantiate the download JSON classdownloadJSON = createDownloadJSONObject()//Downloads and parses the file correctlydownloadJSON.downloadJSON(url: "http://www.example.com/jsonfile.json")//happy days