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
How to Get Wkwebview to Work in Swift and for an Macos App
Is There Any Difference at All Between Suffix(From:) and Dropfirst(_:)
Shorthand for Wrapping a Swift Variable in an Optional
Set an Horizontal Scroll to My Barchart in Swift
Why Do I Get Source Kit Service Terminated Error
How to Refresh The Google Map in My Application Using Swift
How to Use @Fetchrequest Outside a View
Navigation Bar Items After Push from Swiftui to UIkit
Decrypted String Always Returning Null
Accessibility Custom Actions Aren't Announced in Swift
Include Dictionary or Array Value in Swift String with Backslash Notation
Load Viewcontroller Swift - Black Screen