nsjsonserialization

NSJSONSerialization not working as expected in a Playground

☆樱花仙子☆ 提交于 2019-11-29 16:13:16
I have to get football game schedule via JSON and extract date of each game of one of the specific universities. I tried: let url = NSURL(string: "SCHOOL URL") let request = NSURLRequest(URL: url!) let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(request){ (data, response, error) -> Void in do{ let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) if let schedule = jsonData["schedule"] as? [[String: AnyObject]]{ for game in schedule{ if let date = game["date"] as? String{ print("\(date)"); } } } } catch let error as NSError{

NSJSONSerialization and Emoji

百般思念 提交于 2019-11-29 11:53:27
I'm currently trying to POST some JSON containing emojis to a python API. I tried feeding the NSJSONSerialization directly with the string containing the emojis from my UITextField but the serializer crashed with no meaningful explanation. Afterwards I tried to do some format conversion and ended up with something like this: NSString *uniText = mytextField.text; NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding]; NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding] ; This basically works except that the resulting UTF-8 is kinda

How to include null value in the JSON via NSJSONSerialization?

孤人 提交于 2019-11-29 10:54:09
问题 I think I get it how to use the NSJSONSerialization over all. The call I am making is: [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error] where parameters is a NSDictionary which includes keys and values to use in the JSON request. Everything is fine and all, until I have to use null as a value instead of an empty string. I can't find a way to set the value as null at all. The example of json that I need to create is: {"target" {"firstname":<firstname>, "lastname":

HTTP POST error Handling in Swift 2

旧时模样 提交于 2019-11-29 07:26:41
问题 I'm new here, and this is my first question... I try to write an App in Swift 2 that makes an HTTP POST request but I can't figure out how to use the new error handling of Swift 2. Can anyone tell me please how to implement the " do-try-catch " error handling of Swift 2 to the code snippet below? (This code snippet uses the old error handling of swift 1.2) func post(params : Dictionary<String, String>, url : String) { var request = NSMutableURLRequest(URL: NSURL(string: url)!) var session =

Converting NSObject to NSDictionary

左心房为你撑大大i 提交于 2019-11-28 11:05:19
Hello I a class of type NSObject: ProductDetails *details = [[ProductDetails alloc] init]; details.name = @"Soap1"; details.color = @"Red"; details.quantity = 4; I want to pass the "details" object to a dictionary. I did, NSDictionary *dict = [NSDictionary dictionaryWithObject:details forKey:@"details"]; I am passing this dict to another method which performs a check on JSONSerialization: if(![NSJSONSerialization isValidJSONObject:dict]) And I am getting a crash on this check. Am I doing anything wrong here? I know that the details I am getting is a JSON object and I am assigning it to the

Serialize custom object to JSON which contains NSMutableArray

好久不见. 提交于 2019-11-28 10:56:27
问题 I'm trying to serialize my Cart object which has an NSMutableArray of items in it but getting an: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Item)' If I'm understanding how this is supposed to work, I need to create an Array of Dictionaries in order for NSJSONSerialization to work correctly. Is that not what I am doing below? My Cart.h: @interface Cart : BaseObject @property (nonatomic, strong) NSString *comp; @property

Cocoa Error 3840 - NSJSONSerialization

隐身守侯 提交于 2019-11-28 10:35:33
问题 I'm trying to parse a JSON string returned from a ASP.NET web service. The return string has been simplified to just this: <anyType d1p1:type="q1:string">[{"Firstname":"Johnny"}]</anyType> When I run the following code in xcode, I get an error of "Error Domain=NSCocoaErrorDomainCode=3840"..."JSON text did not start with array or object and option to allow fragments are not set" NSURL *url = [NSURL URLWithString:@"http://webserver.com/Service.asmx/GetNames"]; NSData *data = [NSData

Unescaped control characters in NSJSONSerialization

回眸只為那壹抹淺笑 提交于 2019-11-28 10:17:05
I have this JSON http://www.progmic.com/ielts/retrive.php that I need to parse. When I do it with NSJSONSerialization, I get "Unescaped control character around character 1981" error. I need to know: What the heck are the unescaped control characters? Is there a list or something? How do I get rid of this error? The easiest way? Thanks in advance. I added this method to remove the unescaped characters from retrieved string: - (NSString *)stringByRemovingControlCharacters: (NSString *)inputString { NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet]; NSRange range = [inputString

NSJSONSerialization serialization of a string containing forward slashes / and HTML is escaped incorrectly

北城余情 提交于 2019-11-28 10:05:32
I am trying to convert some simple HTML into a string value in a JSON object and I'm having trouble getting the string encoding to not escape the string in NSJSONSerialization. Example... I have a string which contains some basic HTML text: NSString *str = @"<html><body><p>Samples / Text</p></body></html>"; The desired outcome is JSON with HTML as the value: { "Title":"My Title", "Instructions":"<html><body><p>Samples / Text</p></body></html>" } I'm using the standard technique to convert an NSDictionary to a NSString containing JSON: NSMutableDictionary *dict = [NSMutableDictionary dictionary

NSJSONSerialization results in EXC_BAD_ACCESS

荒凉一梦 提交于 2019-11-28 09:22:15
Currently I am writing an app (Target iOS 6, ARC enabled) that uses JSON for data transmission and Core Data for persistent storage. The JSON data is generated out of a MySQL database by a PHP script via json_encode. My Problem is that with data from certain tables the following code fails: - (NSDictionary *)executeFetch:(NSString *)query { NSURL *requesturl = [NSURL URLWithString:[query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSError *dataError = nil; self.jsonData = [NSData dataWithContentsOfURL:requesturl options:kNilOptions error:&dataError]; NSError *error = nil;