nsjsonserialization

NSJSONSerialization from NSString

廉价感情. 提交于 2019-11-26 16:04:16
问题 Is it possible if I have a NSString and I want to use NSJSONSerialization? How do I do this? 回答1: First you will need to convert your NSString to NSData by doing the following NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding]; then simply use the JSONObjectWithData method to convert it to JSON id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 回答2: You need to convert your NSString to NSData , at that point you can use the +[NSJSONSerialization

Reading in a JSON File Using Swift

…衆ロ難τιáo~ 提交于 2019-11-26 15:38:35
I'm really struggling with trying to read a JSON file into Swift so I can play around with it. I've spent the best part of 2 days re-searching and trying different methods but no luck as of yet so I have signed up to StackOverFlow to see if anyone can point me in the right direction..... My JSON file is called test.json and contains the following: { "person":[ { "name": "Bob", "age": "16", "employed": "No" }, { "name": "Vinny", "age": "56", "employed": "Yes" } ] } The file is stored in the documents directly and I access it using the following code: let file = "test.json" let dirs : String[] =

JSON.NET serialize JObject while ignoring null properties

纵然是瞬间 提交于 2019-11-26 14:22:30
问题 I have a JObject which is used as a template for calling RESTful web services. This JObject gets created via a parser and since it's used as a template telling the user what the endpoint schema looks like, I had to figure out a way to preserve all properties, which is why I'm defaulting their values to null . As as example, this is what the object originally looks like: { "Foo":{ "P1":null, "P2":null, "P3":null, "P4":{ "P1":null, "P2":null, "P3":null, }, "FooArray":[ { "F1":null, "F2":null,

how to prevent NSJSONSerialization from adding extra escapes in URL

荒凉一梦 提交于 2019-11-26 12:29:27
问题 How do I prevent NSJSONSerialization from adding extra backslashes to my URL strings? NSDictionary *info = @{@\"myURL\":@\"http://www.example.com/test\"}; NSData data = [NSJSONSerialization dataWithJSONObject:info options:0 error:NULL]; NSString *string = [[NSString alloc] initWithData:policyData encoding:NSUTF8StringEncoding]; NSLog(@\"%@\", string);//{\"myURL\":\"http:\\/\\/www.example.com\\/test\"} I can strip the backslashes and use that string but I would like to skip that step if

How to convert a JSON string to a dictionary?

对着背影说爱祢 提交于 2019-11-25 23:18:47
问题 I want to make one function in my swift project that converts String to Dictionary json format but I got one error: Cannot convert expression\'s type (@lvalue NSData,options:IntegerLitralConvertible ... This is my code: func convertStringToDictionary (text:String) -> Dictionary<String,String> { var data :NSData = text.dataUsingEncoding(NSUTF8StringEncoding)! var json :Dictionary = NSJSONSerialization.JSONObjectWithData(data, options:0, error: nil) return json } I make this function in