Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS JSON Error: NSDebugDescription=Garbage at end

This is a really weird bug, when grabbing JSON from my server (which is produced via PHP), I get this error when calling:

json = [NSJSONSerialization JSONObjectWithData:kivaData
                                       options:kNilOptions
                                         error:&jsonError];

JSON Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x178467d00 {NSDebugDescription=Garbage at end.}

My (NSData* kivaData) grabs everything perfectly, but it cant parse the JSON.

I have run my JSON code in http://jsonlint.com/ and it comes out Valid everytime.

Its really weird because it can parse the JSON when I connect to Wifi, but when I try doing it via cellular, it wont work. It does work over cellular on some peoples phones, but every time.

like image 539
Robert Richardson Avatar asked May 09 '26 23:05

Robert Richardson


1 Answers

using swift 4, first of all check the JSON Data using print :

print (String(data:data!, encoding: .utf8)!)

check for the white spaces or unwanted characters, then remove them :

var string = String(data: data!, encoding: .utf8)
string = string?.replacingOccurrences(of: "/r/n", with: "")

after that, assign the string back to data variable :

let data1 = string!.data(using: .utf8)
like image 161
Ali Alzahrani Avatar answered May 11 '26 13:05

Ali Alzahrani