nsjsonserialization

Swift Dynamic Cast Failed on NSJSONSerialization

我与影子孤独终老i 提交于 2019-12-06 14:01:16
问题 I have a JSON Data which looks like: [ { "Name" : "Ernst Handel", "City" : "Graz", "Country" : "Austria" }, { "Name" : "Wolski Zajazd", "City" : "Warszawa", "Country" : "Poland" } ] and I am converting it to NSDictionary using: var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData( data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary 回答1: I usually use this. Works for both numeric and associative types of JSON. It works for your as well. I tried.

Parsing JSON with NSJSONSerialization: Error 3840 - Data Corrupted?

匆匆过客 提交于 2019-12-06 02:36:06
I've read many Q/As on this problem but couldn't find an answer that fits my situation. I retrieve a JSON response from a REST service I've created in PHP. This is my code: NSURLResponse *response = nil; NSError *theError1 = nil; NSError *theError2 = nil; NSURL *webServiceUrl = [NSURL URLWithString:@"http://..."]; NSURLRequest *request = [NSURLRequest requestWithURL:webServiceUrl cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30]; NSData *theData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&theError1]; NSString *dataString = [[NSString

FHSTwitterEngine - 'NSInvalidArgumentException','data parameter is nil'

╄→гoц情女王★ 提交于 2019-12-05 20:24:22
I'm using FHSTwitterEngine to post a gif to twitpic. When I have a wifi or 3G connection on the iphone everything works fine. But I also want to implement some error handling for when there is no connection or when the upload failed. So for testing I put the iphone in airplane mode and try to upload to twitpic using the following method: id returned = [[FHSTwitterEngine sharedEngine] uploadImageToTwitPic:gif withMessage:@"message" twitPicAPIKey:@"key"]; but when I do that I immediately get the following error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:

iOS - NSJSONSerialization: Unable to convert data to string around character

夙愿已清 提交于 2019-12-05 17:33:40
I'm getting this error while parsing JSON: NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unable to convert data to string around character 73053.) UserInfo=0x1d5d8250 {NSDebugDescription=Unable to convert data to string around character 73053.} Any suggestions how to fix this? ADDED As it says in error report, the parser can't go through the character at position 73053, which is "ø" in my JSON response. As far as I know

What is the more elegant way to serialize my own objects with arrays in Swift

蹲街弑〆低调 提交于 2019-12-05 12:46:34
I have a classes that looks like this: class Foo { var bar = Int() } class Bar { var baz = String() var arr = [Foo]() } and I have an object of Bar structure that I need to serialize to JSON: let instance = Bar() What is the more elegant way to do it via standard library or some third-party libraries? Thanks in advance. I suggest taking this approach: class Foo { var bar = Int() } class Bar { var baz = String() var arr = [Foo]() var jsonDictionary: NSDictionary { return [ "baz" : self.baz, "arr" : self.arr.map{ $0.bar } ] } } let bar = Bar() bar.baz = "some baz" bar.arr.append(Foo()) var error

NSJSONSerialization returns “<null>” string

旧巷老猫 提交于 2019-12-05 08:51:32
I'm try to set an NSDictionary to a JSON object retrieved from the server, I'm doing that in this line: _peopleArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; It works fine and properly creates the dictionary. However, I have a problem, values that are null in the JSON object are stored as "<null>" string values in the dictionary. Is there any way to fix this or work around it? I want to avoid traversing through the entire thing and setting them to @"" . Thanks for any help! ~Carpetfizz There is nothing I guess, though it can be easily corrected from api makers, if

NSURLSessionDownloadTaskDelegate JSON response

99封情书 提交于 2019-12-05 06:49:49
I am running a background NSURLSession session and i am trying to figure out a way to get the JSON response out of one of the NSURLDownloadTaskDelegate callbacks. I have configured my session to accept JSON responses. NSURLSessionConfiguration *backgroundSession = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.Att.Locker.BackgroundUpload"]; backgroundSession.HTTPAdditionalHeaders = @{ @"Accept":@"application/json"}; session = [NSURLSession sessionWithConfiguration:backgroundSession delegate:uploader delegateQueue:nil]; I can easily parse JSON response for

<extracting data from value failed> in NSDictionary

不打扰是莪最后的温柔 提交于 2019-12-04 19:09:57
问题 I am stuck at a point when I convert data from web services to NSDictionary. But while accessing on console in debug mode it returns , while when i bind the values of dictionary with view it works perfectly. Below is the code:- NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&e]; and at console i am using po [dictName valueForKey:@"Status"] as well as po [dictName objectForKey:@"Status"] . Followed few steps but does not work

NSJSONSerialization parsing response data

℡╲_俬逩灬. 提交于 2019-12-04 18:23:38
I created a WCF service, which provides the following response to my POST operation: "[{\"Id\":1,\"Name\":\"Michael\"},{\"Id\":2,\"Name\":\"John\"}]" My call to JSONObjectWithData, doesn't return any error, yet I can't enumerate over the results, what am I doing wrong? NSError *jsonParsingError = nil; NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParsingError]; NSLog(@"jsonList: %@", jsonArray); if(!jsonArray) { NSLog(@"Error parsing JSON:%@", jsonParsingError); } else { // Exception thrown

Swift Dynamic Cast Failed on NSJSONSerialization

北城以北 提交于 2019-12-04 18:17:29
I have a JSON Data which looks like: [ { "Name" : "Ernst Handel", "City" : "Graz", "Country" : "Austria" }, { "Name" : "Wolski Zajazd", "City" : "Warszawa", "Country" : "Poland" } ] and I am converting it to NSDictionary using: var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData( data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary I usually use this. Works for both numeric and associative types of JSON. It works for your as well. I tried. func JSONParseArray(jsonString: String) -> [AnyObject] { if let data = jsonString.dataUsingEncoding