Nsjsonserialization Error. Code=3840 "Invalid Value Around Character 0

NSJSONSerialization error. Code=3840 Invalid value around character 0

123

is a valid JSON number, so this can be read as JSON if the .AllowFragments
option is set. JSON strings must be enclosed in quotation marks:
(see http://www.json.org for the details):

"abc"

In a Swift string literal, these quotation marks are escaped with
backslashes:

let str = "\"abc\"" // OK!
let strData = str.dataUsingEncoding(NSUTF8StringEncoding)
// ...

Alamofire invalid value around character 0

I also faced same issue. I tried responseString instead of responseJSON and it worked. I guess this is a bug in Alamofire with using it with django.

Error (Error Domain=NSCocoaErrorDomain Code=3840 Invalid value around character 0. UserInfo={NSDebugDescription=Invalid value around character 0.}

There is an issue with my web service. They are giving me the response in "text/HTML" format rather than HTML. When i printed my response on debugger then i got:

"Content-Type" = "text/html; charset=UTF-8";

Now, i updated my webservice and everything is working like a charm.

Error Domain=NSCocoaErrorDomain Code=3840 Invalid value around character 0. UserInfo={NSDebugDescription=Invalid value around character 0

First, remove text "Successfully connect" in your PHP code. And the result is not in json format so far. Try to improve it.

Second , at iOS side, you directly use url string as JSON. The right way is to get data from the url, and then convert it to JSON. Try:

NSError *error = nil;
NSString *url_string = @"http://127.0.0.1/RetriveData.php";
NSURL *url = [NSURL URLWithString:url_string];
if (url != nil) {
NSData *data = [NSData dataWithContentsOfURL:url];
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",error);
NSLog(@"json: %@", json);
}


Related Topics



Leave a reply



Submit