5

I'm starting a new project with SBJson parser, which people seem to recommend as the best on the internet for new iOS projects. I'm having a really strage issue which is that the current methods Stig Brautaset claims you can use on the current release (3.1), seem to be deprecated, or at least thats what my compiler is saying. I cant seem to get either to work:

NSDictionary *dict = [responseString JSONValue];

Which seems to be the most current way to do this or:

NSDictionary *dict = [parser objectWithString:responseString error:&error];

Where parser is a sbjson parser. XCode highlights both of these functions and tells me they are deprecated.

What am I doing wrong here??

Jameo
  • 4,507
  • 8
  • 40
  • 66
  • 2
    Why don't you use apple's NSJSONSerialization class ? – Midhun MP Jan 03 '13 at 16:46
  • [JSONKit](https://github.com/johnezang/JSONKit/) is the best parser. NSJSONSerialization is ok if you don't want to use external code and target devices with iOS 5.0 and higher. – mspasov Jan 03 '13 at 17:42
  • If you don't care about iOS4.x and below (and who does, for new projects?) I recommend NSJSONSerialization, unless you specifically need streaming support. However, the current version of SBJson is actually 3.2, and the deprecation of the methods you mention are [well documented](http://sbjson.org/news/2013/sbjson-v3.2.html). – Stig Brautaset Jan 21 '13 at 22:52

3 Answers3

9

Looking at the source code here and here it looks that both

- (id)objectWithString:(NSString*)jsonText error:(NSError**)error

and

- (id)JSONValue;

are deprecated since version 3.2 and it will be removed in version 4.0. Are you sure you are using the 3.1?

On the other hand both

- (id)objectWithString:(NSString *)repr;

and

- (id)objectWithData:(NSData*)data;

look available and not deprecated.

I would suggest you to use them instead.

An alternative is to use the NSJSONSerialization class provided by Apple.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • Thanks, I ended up using NSJSONSerialization. Not sure what the deal with SBJson is, but I definitely had the newest version. The documentation is kindof sketchy over there – Jameo Jan 03 '13 at 17:11
  • You had the newer version, which apparently is the 3.2. It lacks a documentation, though, that's why you didn't see the deprecation warning. – Gabriele Petronella Jan 03 '13 at 17:50
  • What documentation is lacking exactly? (I ask only so I can improve it.) See http://sbjson.org/news/2013/sbjson-v3.2.html and e.g. the warning here: http://sbjson.org/api/3.2/Classes/SBJsonParser.html#//api/name/objectWithString:error: – Stig Brautaset Jan 21 '13 at 22:55
  • 4
    Stig, yes you did document it but rather than just describe that something is deprecated, better to describe what can be used in it's place. Just a thought. – wuf810 Aug 22 '13 at 08:35
0

Just a general way to get things solved easily with xcode it self. Whenever any line of code is deprecated it is best to hold down the "alt" key on your keyboard and move over the object raising the issue and click. Xcode then suggests the better way to write your code. Hope this helps

John Stone
  • 75
  • 10
0

Try to replace:

NSDictionary *dict = [responseString JSONValue];

With:

NSDictionary *dict = [[SBJsonParser new] objectWithString:responseString];

Or better:

SBJsonParser *parser = [SBJsonParser new];
id object = [parser objectWithString:responseString];
if (parser.error || ![object isKindOfClass:[NSDictionary self]])
    @throw parser.error ?: @"not a json dictionary";// or any error handling alternative
NSDictionary *dict = object;
Cœur
  • 37,241
  • 25
  • 195
  • 267