nsjsonserialization

Serialize JSON string that contains escaped (backslash and double quote) Swift return Badly formed object

本小妞迷上赌 提交于 2020-01-01 12:01:39
问题 I have response string from the backend like this: { "status": "success", "data": "{\"name\":\"asd\",\"address\":\"Street 1st\"}" } I think the problem was because the double quote (") in the data String. When I remove the double quote, the serialization was success. But the response is from backend and I have to deal with it. Anyone can solve this problem? Thank you. Here is the playground code. import Foundation var jsonStr = """ { "status": "success", "data": "{\"name\":\"asd\",\"address\"

iOS-Swift : Serialize a NSManagedObject to JSON (with relationships)

て烟熏妆下的殇ゞ 提交于 2020-01-01 09:55:29
问题 After spending too much time trying to find the best practices, here I am once again asking for some help, hoping I am not the only one struggling with this : I have NSManaged Objects like these : import Foundation import CoreData class Credential: NSManagedObject { @NSManaged var credentialArrivalDate: String? @NSManaged var credentialBarCode: String? @NSManaged var credentialComment: String? @NSManaged var credentialCountCheckIn: Int @NSManaged var credentialCountCheckOut: Int @NSManaged

How to serialize UIImage to JSON?

青春壹個敷衍的年華 提交于 2019-12-31 22:40:09
问题 I am using imageData = UIImagePNGRepresentation(imgvw.image); and while posting [dic setObject:imagedata forKey:@"image"]; after NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&theError]; now app is crashing Terminating app due to uncaught exception ' NSInvalidArgumentException ', reason: 'Invalid type in JSON write (NSConcreteMutableData) 回答1: You need to convert your UIImage to NSData and then convert that NSData to a NSString which will

Swift 4: Keeping the same order when parsing a JSON

会有一股神秘感。 提交于 2019-12-31 05:07:39
问题 I need to show a list of addresses in my app while keeping the same order as the response JSON. It looks something like this: { "addresses": { "e5fdb5ba-7afb-11e8-bead-43321d1a8905": { "contact": "Name1", "zipCode": "06100" }, "1495b580-7afc-11e8-bead-9f4049791561": { "contact": "Name2", "zipCode": "06100" }, "e5fdb5ba-7afb-11e8-bead-43321d1a8905": { "contact": "Name3", "zipCode": "06100" }, "1495b580-7afc-11e8-bead-9f4049791561": { "contact": "Name4", "zipCode": "06100" } } } But when I

How to create true/false in JSON in Objective-C

萝らか妹 提交于 2019-12-30 10:15:11
问题 How do I get true/false (instead of "true"/"false") in json from a NSDictionary using NSJSONSerialization dataWithJSONObject? What keys should I store in the dictionary to get that? 回答1: NSNumber objects containing a BOOL are mapped to JSON "true" and "false". So just use @YES , @NO , or generally @(someBOOL) . For example: NSDictionary *dict = @{@"this is": @YES}; NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL]; NSString *str = [[NSString alloc] initWithData

Unescaped control characters in NSJSONSerialization

浪子不回头ぞ 提交于 2019-12-28 16:02:29
问题 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. 回答1: I added this method to remove the unescaped characters from retrieved string: - (NSString *)stringByRemovingControlCharacters: (NSString *

Newtonsoft inline formatting for subelement while serializing

血红的双手。 提交于 2019-12-28 04:33:07
问题 Is it possible to create an attribute to serialize some subelements inline (Formatting.None) with newtonsoft json.net? I have a very huge set of data and I want to keep it readeable. Some subelements are not very important and can be writen inline. { "name": "xxx", "desc": "xxx", "subelem": [ {"val1": 1, "val2": 2, ...}, //inline, {"val1": 1, "val2": 2, ...}, ... ] "subelem2": { "val1": 1, "val2": 2, ... } } I want to force the inline serialization for some sub objects of my models. In this

iOS Convert NSData to NSDictionary returns nil?

心不动则不痛 提交于 2019-12-25 02:29:11
问题 In My code I want to convert NSData to NSDictionary but it returns nil I don't know what mistake I made,I Used NSJSONSerialization for convert data to dictionary, The NSData was received from server response. Here I show my Full code what I am trying. -(void)SendPushNotification:(NSString*)getUrl :(NSMutableDictionary *)getData withCompletionBlock:(void(^)(NSDictionary *))completionBlock { NSError *error; NSLog(@"dict val: %@",getData); NSData *jsonData = [NSJSONSerialization

How to use Swift's JSONSerialization when JSON returns a single string

别说谁变了你拦得住时间么 提交于 2019-12-24 20:25:19
问题 So this is likely a very basic question, but I was curious how you would handle parsing a JSON object that returns as a single string using JSONSerialization in Swift. So I'm playing around with a free Harry Potter API and noticed that one of the routes returns a single string (https://www.potterapi.com/v1/sortinghat) and the response it gives is simply a single string of one of the four houses in Harry Potter. "Ravenclaw" Below was my attempt. do { let json = try JSONSerialization.jsonObject

NSJSONSerialization Not Working, NSUTF8StringEncoding Is Working

元气小坏坏 提交于 2019-12-24 05:09:51
问题 I've read the documentation, tutorials, and other Stack Q&A and am hung up on how I'm able to get my JSON to appear using NSUTF8StringEncoding , but not using NSJSONSerialization - which is what I need to extract the data to a NSDictionary or NSArray . I believe it is an issue with the format of the NSData being returned from my URL? EDIT Here is error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x8aabc30