在iOS中解析连续的JSON流 [英] Parsing continuous JSON stream in iOS

查看:135
本文介绍了在iOS中解析连续的JSON流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力为我正在撰写的应用程序获取JSON。在应用程序端,我有一个NSInputStream,它连接到一个带有CFStreamCreatePairWithSocketToHost的服务器。

I'm struggling to get a hang of JSON for an app I'm writing. On the app side I have a NSInputStream that is connected to a server with CFStreamCreatePairWithSocketToHost.

服务器以异步方式为应用程序生成JSON对象。

The server is generating JSON objects in an async fashion to the app.

在应用程序中,我对事件NSStreamEventHasBytesAvailable上的网络数据作出反应。在某些网络上,我体验到我在网络缓冲区中收到了多个JSON对象。但是我也想要处理我在一个网络缓冲区中没有收到整个JSON对象的情况。

In the app I react to network data on the event NSStreamEventHasBytesAvailable. On some networks I experience that I receive multiple JSON objects in the network buffer. But I also want to be take care of the scenario where I do not receive the entire JSON object in one network buffer.

我一直在寻找一个JSON解析器,将为我处理这些情况,但一直无法找到。 NSJSONSerialization无法很好地处理要传递的NSData中的多个JSON对象。我无法理解如何让NSJSONSerialization在一个流上工作,我不确定它能解决我的问题。

I've been looking for a JSON parser that will handle these scenarios for me, but haven't been able to find one. NSJSONSerialization doesn't cope well with multiple JSON objects in the NSData it is to pass. I can't get the hang of how to get NSJSONSerialization working on a stream and am unsure it that will solve my problem.

我已经调查了YAJL但我可以只让它不止一次工作。我似乎找不到任何好的例子。

I've looked into YAJL but I can only get it to work more than once. I can't seem to find any good examples for the scenario I have.

我很沮丧和困惑什么是最好的方法,我在哪里找到一个很好的例子?欢迎任何建议!

I'm frustrated and confused what is the best approach and where I find a good example? Any suggestions are welcome!

推荐答案

有一个名为 SBJson

这是一个例子(从这里开始):

- (IBAction)go {
    id block = ^(id item, BOOL *stop) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // do something with item
        });
    };

    id eh = ^(NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // handle error
        });
    };
    self.parser = [SBJson4Parser unwrapRootArrayParserWithBlock:block
                                                   errorHandler:errorHandler];

    NSURLSessionConfiguration *c = [NSURLSessionConfiguration defaultSessionConfiguration]
    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:c
                                                             delegate:self
                                                        delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:self.urlField.text];
    NSURLSessionDataTask *urlSessionDataTask = [urlSession dataTaskWithURL:url];
    [urlSessionDataTask resume];
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
    didReceiveData:(NSData *)data {
    switch ([self.parser parse:data]) {
        case SBJson4ParserError:
            self.parser = nil;
            break;
        case SBJson4ParserComplete:
        case SBJson4ParserStopped:
            self.parser = nil;
            break;
        case SBJson4ParserWaitingForData:
            break;
    }
}

这篇关于在iOS中解析连续的JSON流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆