user1028028
user1028028

Reputation: 6433

Parse NSString with SBJSON

Quick question:

How do I parse a NSString using SBJSON4 The string is an UTF-8 encoded JSON string from a web REST api. I need an NSDIctionary with the parsed data. The string is guaranteed to be a complete JSON document.

   @interface NSOperationParseJSON ()

@property (weak, nonatomic) id<JSONParseDelegate> delegate;
@property (strong, nonatomic) NSString *stringToParse;
@property (strong, nonatomic) SBJson4Parser *jsonParser;

@end

@implementation NSOperationParseJSON

- (instancetype)initWithJSONString:(NSString *)jsonString andDelegate:(id<JSONParseDelegate>)delegate
{
    self = [super init];
    if (self) {
        _delegate = delegate;
        _stringToParse = jsonString;
        _jsonParser = [[SBJson4Parser alloc] init];
    }
    return self;
}

#pragma mark - OVERRIDEN 

- (void)main
{
    @autoreleasepool {
        if (self.isCancelled) {
            return;
        }

        SBJson4ParserStatus responseCode = [self.jsonParser parse:[self.stringToParse dataUsingEncoding:NSUTF8StringEncoding]];

        if (responseCode == SBJson4ParserComplete) {

        } else if (SBJson4ParserError) {

        }

    }
}

Where do I get the response?

Upvotes: 0

Views: 1187

Answers (1)

Stig Brautaset
Stig Brautaset

Reputation: 2632

You will need to read up on the documentation. Version 4’s interface is quite different from previous versions. Although it supports some modes of working that NSJSONSerialisation does not, such as working with uncomplete JSON streams, it doesn't support non-standard JSON (unless there is a bug I missed).

If you still want to explore it, here are two examples:

https://github.com/stig/ChunkedDelivery or https://github.com/stig/DisplayPretty

Upvotes: 0

Related Questions