【问题标题】:Parse NSString with SBJSON使用 SBJSON 解析 NSString
【发布时间】:2014-08-05 08:41:45
【问题描述】:

快速提问:

如何使用 SBJSON4 解析 NSString 该字符串是来自 Web REST api 的 UTF-8 编码 JSON 字符串。 我需要一个带有解析数据的 NSDIctionary。该字符串保证是一个完整的 JSON 文档。

   @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) {

        }

    }
}

我从哪里得到响应?

【问题讨论】:

  • 你为什么不使用 NSJSON ?
  • 非标准 json。我有一些类似“@data”的键......而 NSJSON 无法处理这些类型的 json。

标签: ios objective-c sbjson


【解决方案1】:

您需要阅读文档。版本 4 的界面与以前的版本有很大不同。尽管它支持 NSJSONSerialisation 不支持的某些工作模式,例如处理不完整的 JSON 流,但它不支持非标准 JSON(除非我错过了一个错误)。

如果你还想探索,这里有两个例子:

https://github.com/stig/ChunkedDeliveryhttps://github.com/stig/DisplayPretty

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多