How to fix JSON text did not start with array or object and option to allow fragments not set."

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}



Here is my code:


let myUrl = NSURL(string: "http://www.01coders.com/ios/userRegister.php");

let request = NSMutableURLRequest(URL : myUrl!);

request.HTTPMethod = "POST";

let postString = "username=\(username)&password=\(password)";

request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

let task = NSURLSession.sharedSession().dataTaskWithRequest(request){

data, response, error -> Void in

if (error != nil){

print("error=\(error)")

return

}

do{

let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary

print("json: \(json)");

let resultValue = json["status"] as! String;

print("Result: \(resultValue)")

var isUserRegistered:Bool = false;

if(resultValue=="Success"){

isUserRegistered = true;

}

var messageTodisplay:String = json["message"] as! String;

if(!isUserRegistered){

messageTodisplay = json["message"] as! String;

}

dispatch_async(dispatch_get_main_queue(), {

let myAlert = UIAlertController(title: "Alert", message: messageTodisplay, preferredStyle: UIAlertControllerStyle.Alert);

let okaction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil);

myAlert.addAction(okaction);

self.presentViewController(myAlert, animated:true, completion:nil);

})

}catch let error as NSError{

print(error)

}

}

task.resume();

Replies

It’s likely that the data you’re getting back isn’t JSON, or is subtly malformed JSON. Double check that the data is what you expect it to be. If you can’t spot the problem, post a hex dump of it. You can can get that with:

NSData("data = %@", data!)

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

DTS will close for the winter holidays at the end of business on Wed, 23 Dec 2015 and re-open on Mon, 4 Jan 2016.

Thank you for your help.


But where I can put this code in my code, can you please check my code and let me know.

before you go thrashing around in your swift code, always run it through the validator, it will save your head from pounding the keyboard


jsonlint.com

sorry I am not getting you, can you please explain me in details because I am new for Swift.

sorry I am not getting you

I believe iPad_dev was suggesting that you run your JSON through a validator. JSON parsers are generally quite strict, so if the server-side code generating the JSON is doing the wrong thing then you’ll see errors like this. Thus, it’s best to check that your JSON is valid, using an automated tool like JSONLint, before you start trying to debug your client code.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

DTS will close for the winter holidays at the end of business on Wed, 23 Dec 2015 and re-open on Mon, 4 Jan 2016.

Did you fix this error?

remove square brackets ( [ , ] )

example output is {"Name" : "Hello"}