Issues getting json from webserver in iOS5 - ios

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.

Related

Why does RKNSJSONSerialization crash on iOS 6?

I'm seeing a crash in RKNSJSONSerialization on iOS 6 only - not on iOS 7. I'm using RestKit 0.20.3 and it happens fairly consistently for me. Even when I make the same request and get the same response for iOS 6/7, it works fine on iOS 7 but crashes on iOS 6.
Here's the crash - it's an EXC_BAD_ACCESS: http://crashes.to/s/2610b639062
The relevant (crashing) line in RestKit's RKNSJSONSerialization is the return:
+ (id)objectFromData:(NSData *)data error:(NSError **)error
{
return [NSJSONSerialization JSONObjectWithData:data options:0 error:error];
}
So perhaps it's not RestKit at all - perhaps it's NSJSONSerialization.
I profiled the app with the Zombies tool and found this:
"An Objective-C message was sent to a deallocated 'CFString (immutable)' object (zombie) at address: 0x16851250."
Am I doing something wrong?
I've resolved this. The issue was that my JSON had duplicate keys in it, and iOS 6 couldn't handle that. The solution is to remove the duplicate keys from the JSON before trying to parse it with NSJSONSerialization on iOS 6. Apparently Apple has resolved this issue on iOS 7, since it doesn't crash there.
Related: NSJSONSerialization bug?
This seems to be a typo.
Assuming there is an object called error of type NSError, the call JSONObjectWithData: takes a pointer to a pointer as the last argument, i.e. with &.
return [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

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

Parsing JSON using NSJSONSerialization

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!

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.

exception error with json in xcode

I'm beginner in ios. I developing a call with JSON for access a data and insert after on UITableView.
In this point on my code
NSDictionary * dict = [[CJSONDeserializer deserializer ] deserialize:jsonData error:&error];
When compiling I obtained this error
2013-02-07 15:49:48.078 comercMobil[12933:c07] +[CJSONScanner scannerWithData:]: unrecognized selector sent to class 0xe7b8
2013-02-07 15:49:48.080 comercMobil[12933:c07] Exception +[CJSONScanner scannerWithData:]: unrecognized selector sent to class 0xe7b8
any suggestions? thanks for all
A couple of things:
This is not a compiler error, but rather a runtime error.
I don't think this is the line that's causing this error, since you're not calling scannerWithData here. I'd search your source for a reference to scannerWithData.
But, I agree with Sean that you should consider just using NSJSONSerialization.
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
Or, if you need support for iOS prior to version 5.0, SBJSON is very popular.

Resources