Convert NSData to JSON

I have an NSData object, I need to convert it to an NSDictionary object.

 NSData *data = ....; 

Now I need to convert this to NSDictionary , how can I do this programmatically?

note: After saving the NSData to NSDictionary I must have access to the NSDictionary key value NSDictionary .

I have no code to demonstrate my work so far, I just created an NSData object and have no way to continue :)

+6
source share
3 answers

You can subclass MKNetworkOperation and override the responseJSON method with the following:

 -(id) responseJSON { NSString *rawJSON; id jsonValue = nil; if ((rawJSON = [[NSString alloc] initWithData:[self responseData] encoding:NSUTF8StringEncoding]) != nil) { SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; if ((jsonValue = [jsonParser objectWithString:rawJSON]) == nil) { NSLog(@"This string doesn't seem to be JSON: '%@'\nraw Data : '%s'", rawJSON, (char *)[[self responseData] bytes]); return [self responseString]; } } else { NSLog(@"This data doesn't seem to be an UTF8 encoded string: %@", [self responseData]); return [self responseString]; } return jsonValue; } 
+3
source

Please check this stack overflow link, I have already used JSON services, this will help you a lot. All encodings are.

JSON Data Conversion

And here is a tutorial with a sample project

JSON Parse Tutorial

I hope you will like it

+3
source

I highly recommend calling the SBJSON structure , it saved my time for a long time, completely finished my work and was easy to use. You do not need to know the details of the conversion algorithm, just download and call it.

You can download it from here , then follow this tutorial to get it done.

+1
source

Source: https://habr.com/ru/post/906664/


All Articles