3

i have copied SBJson folder into my project and also #import "SBJson.h"

But still i am not getting

NSDictionary *result = [strResult JSONValue];

Even Xcode does not show any option JSONValue;

even if i write JSONValue than it prompt me error

No visible @interface for 'NSString' declares the selector 'JSONValue'
madLokesh
  • 1,860
  • 23
  • 49
Chitra Khatri
  • 1,260
  • 2
  • 14
  • 31

2 Answers2

18

You don't need SBJson for this.

There is a native class NSJSONSerialization that does this much faster and without the need to import anything.

NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[strResult dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

A better way would just be to use the NSData straight from he request...

NSDictionary *result = [NSJSONSerialization JSONObjectWithData:theJSONDataFromTheRequest options:0 error:nil];
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 5
    This is how we should be parsing JSON now. SBJson was created for the times before we had built in JSON serialization. – LJ Wilson Jul 17 '13 at 10:12
  • `NSString *response = [NSString stringWithFormat:@"http://www.....php"]; const char *convert = [response UTF8String]; NSString *responseString = [NSString stringWithUTF8String:convert]; NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; NSArray *ads = [responseString JSONValue]; NSLog(@"%@",[[ads objectAtIndex:0] objectAtIndex:1]);` Why this code not working @fogmeister – Gökhan Çokkeçeci Mar 21 '14 at 15:10
  • @gokcokkececi what are you trying to do? The string you have is just text not JSON. You are not getting anything from the web here. – Fogmeister Mar 21 '14 at 17:04
  • strResult is what ? is it NSData or NSString or NSURL or NSDIctionary – Rahul Feb 04 '15 at 05:45
  • @Fogmeister please suggest – Rahul Feb 04 '15 at 05:46
  • @UnicoRahul `strResult` is taken from the OP's question and is an NSString in my case. I have edited the answer to show how you can just use the NSData though. – Fogmeister Feb 04 '15 at 10:27
0

If you still want to use SBJson the replace #import "SBJSON.h" with #import "JSON.h" and you will get it working.

  • 4
    As the author of SBJson I don't understand why you would say that. This would mean that you're using a release that is over 3 years old, rather than use a recent release. Anyway, unless you need to support iOS4 or lower you should just use Apple's `NSJSONSerialisation`. – Stig Brautaset Aug 08 '13 at 21:17