nsjsonserialization

Creating an object that works with NSJSONSerialization dataWithJSONObject:options:error:

余生长醉 提交于 2019-12-23 12:07:00
问题 At the moment in order to use this function I'm "converting" my object into a dictionary. i.e. the property names become the keys and the property values become the values. Is there a way to properly do this so that an object will work with this function? i.e. similar to encodeWithCoder and initWithCoder for use with NSUserDefaults . I'd like to do ... NSData *data = [NSJSONSerialization dataWithJSONObject:myObject options:0 error:nil]; at the moment I do something like... NSData *data =

Invalid value around character 0, NSJSONSerialization

假如想象 提交于 2019-12-23 08:27:37
问题 I make a get from my server and I get a valid response: - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Append the new data to the instance variable you declared //[_responseData appendData:data]; NSString *responseBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",responseBody); if(data != NULL) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) { NSError *error =

iOS 5 JSON to tableView error

左心房为你撑大大i 提交于 2019-12-23 02:22:09
问题 I was trying to parse a JSON string from USGS. However I get error "-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x68a34c0" Basically, I would like to parse the USGS JSON into the tableview. Can anyone help? Here are my codes. ViewController.h @interface EarthquakeViewController : UITableViewController { NSArray *json; NSDictionary *earthquakeReport; } ViewController.m - (void)viewDidLoad { //content = [NSMutableArray arrayWithObjects:@"one", @"two", nil]; [super

iPhone - NSURLConnection asynchronous download using URLs in NSArray

北城以北 提交于 2019-12-23 01:41:17
问题 I have seen almost all the posts about NSURL on this site, and I am still stuck. I am using Xcode 4.5. I am trying to download images and display them in a UIScrollView. I want to download asynchronously download images using URLs, that get stored in an array populated using JSON. I get the URLs from a JSON grab off of my database. That works quite well and I can see the URL's being placed into the urlArray, but making the URLConnection to get the image, seems to fail. I can't get any of the

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

六月ゝ 毕业季﹏ 提交于 2019-12-22 09:31:10
问题 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

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

和自甴很熟 提交于 2019-12-22 08:54:56
问题 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. 回答1: I suggest taking this approach: class Foo { var bar = Int() } class Bar { var baz = String() var arr = [Foo]() var jsonDictionary: NSDictionary { return [ "baz" : self

Posting http JSON request using NSJSONSerializer iOS

做~自己de王妃 提交于 2019-12-21 20:58:03
问题 Here is my code: In my .m NSArray *keys = [NSArray arrayWithObjects:@"Training_Code", @"Training_Duration",@"Training_Startdate",@"Training_Enddate",@"Trainer_ID",@"Training_Location",@"Comments",@"Keyword",@"NumberofDays", nil]; NSArray *objects = [NSArray arrayWithObjects:@"Training_Code", @"Training_Duration",@"Training_Startdate",@"Training_Enddate",@"Trainer_ID",@"Training_Location",@"Comments",@"Keyword",@"NumberofDays", nil]; NSData *jsonData; NSString *jsonString; NSDictionary

Issue in converting NSDictionary to json string, replacing / with \/

限于喜欢 提交于 2019-12-21 09:25:26
问题 i want to convert NSDictionary to json string.everything is working fine, i have a small issue that is described as follows: I have a following code for conversion of NSDictionary to NSString: -(NSString *)dictToJson:(NSDictionary *)dict { NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; return [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding]; } I am calling the method as:

Cannot parsing Json to NSDictionary

寵の児 提交于 2019-12-20 07:07:32
问题 I have a WebService, which give me the fallowing Json-String back: "{\"password\" : \"1234\", \"user\" : \"andreas\"}" I call the webservice and try to parse the returned data like: [NSURLConnection sendAsynchronousRequest: request queue: queue completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) { if (error || !data) { // Handle the error } else { // Handle the success NSError *errorJson = nil; NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData: data

how to create json in objective-c

泄露秘密 提交于 2019-12-18 12:54:07
问题 NSData* jsonDataToSendTheServer; NSDictionary *setUser = [NSDictionary dictionaryWithObjectsAndKeys:[@"u" stringByAppendingString:my.id],@"id", @"GET_USER_INFO",@"command", @"",@"value", nil]; NSLog(@"%@", jsonDataToSendTheServer); Here is my code. When I run above code I get this print <7b226964 223a2275 35383738 37373334 31222c22 636f6d6d 616e6422 3a224745 545f5553 45525f49 4e464f22 2c227661 6c756522 3a22227d> I don't have any idea whether I can created a json or not. How can I fix this?