简体   繁体   English

在IOS上使用AFNetworking流式传输JSON

[英]Streaming JSON with AFNetworking on IOS

What would be the most elegant way to receive data from a streaming JSON API using AFNetworking? 使用AFNetworking从流式JSON API接收数据的最优雅方式是什么? AFNetworking provides excellent support for receiving non-streaming data from JSON APIs, but I couldn't find any examples of streaming JSON. AFNetworking为从JSON API接收非流数据提供了出色的支持,但我找不到任何流JSON的例子。

AFNetworking does not have a built-in streaming SAX-style JSON operation, but it should be straight forward to create your own. AFNetworking没有内置的流式SAX风格的JSON操作,但它应该是直接创建自己的。

  • Create a subclass of AFJSONRequestOperation 创建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 ). 在初始化期间使用操作的outputStream属性将其连接到支持SAX样式解析的JSON解析器(例如Yajl )。 The parser will read data and build up the JSON object as it comes in 解析器将读取数据并在其进入​​时构建JSON对象
  • Hook up the responseJSON property to read the cached object from the parser 连接responseJSON属性以从解析器中读取缓存的对象
  • ...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 (eg setReceivedJSONBlock:((^)(void (id JSON))block ). This block will probably be triggered by delegate methods sent from the parser (eg <YAJLParserDelegate> -parserDidEndDictionary: ). ...或者取决于你如何进行流式处理,你可能需要添加一个新属性来定义一个块,以便在新的JSON对象返回时执行(例如setReceivedJSONBlock:((^)(void (id JSON))block )这个块可能是由解析器发送的委托方法触发的(例如<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. 我认为这可能对很多人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM