在IOS上使用AFNetworking流式传输JSON [英] Streaming JSON with AFNetworking on IOS

查看:281
本文介绍了在IOS上使用AFNetworking流式传输JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AFNetworking从流式JSON API接收数据的最佳方式是什么? AFNetworking为从JSON API接收非流数据提供了出色的支持,但我找不到任何流JSON的例子。

What would be the most elegant way to receive data from a streaming JSON API using AFNetworking? AFNetworking provides excellent support for receiving non-streaming data from JSON APIs, but I couldn't find any examples of streaming JSON.

推荐答案

AFNetworking没有内置的流式SAX风格的JSON操作,但它应该直接创建你自己的。

AFNetworking does not have a built-in streaming SAX-style JSON operation, but it should be straight forward to create your own.


  • 创建 AFJSONRequestOperation的子类

  • 在初始化期间使用操作的 outputStream 属性将它连接到支持SAX样式解析的JSON解析器(例如 Yajl )。解析器将读取数据并构建JSON对象。

  • 连接 responseJSON 属性以从中读取缓存对象解析器

  • ...或者根据您的流式传输方式,您可能需要添加一个新属性来定义在新的JSON对象返回时执行的块(例如 setReceivedJSONBlock:((^)(void(id JSON))block )。这个块可能会被解析器发送的委托方法触发(例如< YAJLParserDelegate> -parserDidEndDictionary:)。

  • Create a subclass of AFJSONRequestOperation
  • Use the outputStream property of the operation during initialization to hook it up to a JSON parser that supports SAX-style parsing (such as Yajl). The parser will read data and build up the JSON object as it comes in
  • Hook up the responseJSON property to read the cached object from the parser
  • ...or depending on how you're doing streaming, you may need to add a new property that defines a block to execute when new JSON objects come back (e.g. setReceivedJSONBlock:((^)(void (id JSON))block). This block will probably be triggered by delegate methods sent from the parser (e.g. <YAJLParserDelegate> -parserDidEndDictionary:).

如果你能够使这个工作,我会鼓励你发布并与他人分享。我认为这可能对很多人有用。

If you are able to get this working, I would encourage you to publish and share this with others. I think this could be useful to quite a few people.

这篇关于在IOS上使用AFNetworking流式传输JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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