iOS JSON Error: NSDebugDescription=Garbage at end

前端 未结 5 883
有刺的猬
有刺的猬 2020-12-21 07:52

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 JSONObj         


        
5条回答
  •  太阳男子
    2020-12-21 08:57

    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)
    

提交回复
热议问题