Parsing JSON using NSJSONSerialization - ios

I am currently going through the Pragmatic iOS 6 book, and am having trouble understanding the following line of code explained in chapter 3 under the section about GCD:
NSJSONSerialization *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
//... some code here
NSArray *tweets = (NSArray *) jsonResponse; //<-- this line
Is it saying that the NSJSONSSerialization object could automatically return an instance of NSSArray, which is then stored in the tweets? I checked the Apple docs, but only saw items on restrictions using NSJSONSerialization, but not what objects it could then get converted to.
Thanks!

NSJSONSerialization can take a chunk of JSON data and turn into objects and it can do the same in the other direction that is by taking objects and converting them into the JSON data.
For easy understanding of NSJSONSerialization and interaction with Twitter, i would recommend you to use THIS and THIS tutorials.
Hope this helps!

Related

Couldn't Serialize NSData Objective C

I couldnt serialize the following JSON data. I can't see anything wrong in it.
[{"id":2182,"title":"HEAVEN ON EARTH","content":"Description","post_date":"2014-07-28 09:29:45","post_modified":"2014-07-28 09:29:45","abc_featured":"","abc_rating":"0","abc_recommended":"","abc_phone":"","abc_address":"","abc_address2":"","abc_postcode":"","abc_suburb":"","abc_state":"NSW","abc_website":"http:\/\/","abc_email":"Pralead#hotmail.com","abc_lat":"-1000","abc_lon":"10000","abc_closed_txt":"","yes_event":"","from_event_date":"","to_event_date":"","abc_location":"","abc_age":"","abc_cost":"","abc_prebook":"","abc_parentaccompany":"","abc_changingfaciltiy":"","abc_playarea":"","abc_pramaccessibility":"","abc_breastfeedingarea":"","abc_refreshments":"","abc_inoutdoor":"","abc_parking":"","abc_freetrail":"","feature_name":null,"feature_value":null,"feature_available":[],"image1":"http:\/\/localhost\/wordpress\/wp-content\/uploads\/2014\/07\/IMG_0014.png","image2":"http:\/\/localhost\/wordpress\/wp-content\/uploads\/2014\/07\/IMG_0011.png","image3":"http:\/\/localhost\/wordpress\/wp-content\/uploads\/2014\/07\/IMG_0009.png","Monday_from_time":"","Monday_to_time":"","Tuesday_from_time":"","Tuesday_to_time":"","Wednesday_from_time":"","Wednesday_to_time":"","Thursday_from_time":"","Thursday_to_time":"","Friday_from_time":"","Friday_to_time":"","Saturday_from_time":"","Saturday_to_time":"","Sunday_from_time":"","Sunday_to_time":""}]
CODE TO SERIALIZE IS:
NSArray *jsonArray=[NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];
_downloadedData is the data I am geting from the JSON API.
I CAN SEE THE DATA COMING THROUGH THE NSLOG.
At a minimum the JSON in the question has problems, the main one is "null" for the value of a dictionary item.
Ex: "feature_name":null.
NSDictionary values must be an object.
Is the JSON in the question a NSLog() of _downloadedData?

iOS RestKit 0.2 What is the best way to parse local Json

I wave read many articles about parsing a json via an Http request but almost none answers the question about what is the most straight forward way to parse o local json string. I found some deferent solutions here Deserializing local NSString of JSON into objects via RestKit (no network download) some of them work some others don't. Is there any "official" RestKit support for local JSON string deserialization?
Fortunately Apple provides "NSJSONSerialization" class from iOS 5.0. to serialise your data.
Step 1 : convert your local JSON string to "NSData"
NSString *strLocalJSON = #"{data= "some data"}"; //just for ex. may not be valid one
NSData *dataJSON = [str dataUsingEncoding:NSUTF8StringEncoding];
Step2:
id jsonobject= [NSJSONSerialization JSONObjectWithData:dataJSON options:NSJSONReadingMutableContainers error:nil]; // resulting object may contain Dictionary or Array depends on your localjson
for further info refer https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html
If your JSON is in a file then you use a file:// NSURL when you create your RestKit object manager. Everything else works as normal.
If the JSON is already in code, then you should save it to a file, or why are you needing to do any mapping? Anyway, you could use a combination of NSJSONSerialization and RKMappingOperation.

JSON Warning in Flickr Photo Manager

I am getting a warning in my iOS app. It doesn't seem to be affecting performance in iOS 6 but my app is crashing when loading the photos from Flickr in iOS 7 beta 2 and I'm not sure if it's an issue with the beta or perhaps something to do with this warning but I'd like to get rid of the warning anyway. Any help?
Implicit conversion from enumeration type 'enum NSJSONWritingOptions' to different enumeration type 'NSJSONReadingOptions' (aka 'enum NSJSONReadingOptions')
NSError *error;
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&error];
Pretty simple !! Just re-read the warning again. It says you are passing NSJSONWritingOptions option in options parameter while it expects NSJSONReadingOptions.
Change your line of code as below:
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
It should help you.
Core Background works on iOS 6 and iOS 7 beta 1,2. The project was inspired by the Yahoo Weather iOS App. ObjectiveFlickr is leveraged for Flickr integration.
https://github.com/justinmfischer/core-background

Is there a data limit when using NSJSONSerialization in iOS?

I am sorry to bother you but I haven't found any usable topic that would help me.
I use NSMutableRequest through NSURLConnection to get my JSON data. Once the data are received, I serialize it using [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error].
It works just fine when getting a smaller JSON files but it doesn't work when I receive bigger JSON file. I write the log NSLog(#"--->: %#", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); and the output is not one but two log records starting with --->:. The first output contains the first part of the JSON and the rest of JSON lays in the second one.
This error won't allow creation of NSDictionary as needed. Every "bigger" JSON received is split somewhere between 7500 - 8000 characters. My question is: Is there a limit that NSJSONSerialization can handle? From my perspective, it seems like it can handle 8 kilobytes of data and that is it. Is there any way to bypass it?
Thank you for your insights.
Thank you guys for your answers. I finally found the solution for my problem. The problem was in saving data badly in didReceiveData method. After applying [receivedData appendData:data]; everything starts to work well. The 8 kB problem is probably a chunk of data sent at once.

Issues getting json from webserver in iOS5

I have a app that receive a feed from a server through json.
I have been building it for iOS5, but in the last weeks testing with iOS6. I tested today with a iOS5 device, and everything crashed.
The code looks like this:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.myserver.com/news.json"]];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
It worked without problems, but now it returns a null. The data is not null.
Cocoa error 3840 means
The data couldn’t be read because it has been corrupted. (No string
key for value in object around character 2.)
You should check your json with a validator like http://www.jsoneditoronline.org/ or http://jsonformatter.curiousconcept.com/
The issue was a duplicated key from the webserver. Now it works.
But it strange that the error is trigged in iOS5 and not in iOS6.

Resources