AS3/Flex/Adobe AIR 应用程序的流式解析器(JSON/XML)

3
有没有可用于AS3的json或xml格式的流读取、解析库?我正在设置一个长轮询应用程序,使用URLStream/URLRequest。除了选择格式之外,我无法控制接收到的数据。我希望有一个可以一次处理片段的解析器,这将允许我在某些完整的片段可用时触发自定义事件。你有什么想法吗?当前的AIR应用程序如何处理这个问题?
示例API:
var decoder:StreamingJSONDecoder = new StreamingJSONDecoder();
decoder.attachEvent("onobjectavailable", read_object); 

while (urlStream.bytesAvailable) 
{
  decoder.readBytes(get_bytes(urlStream)); 
}
3个回答

1

当前的AIR版本(v2.5)捆绑了一个更新的WebKit,通过JSON.stringify()和JSON.parse()提供本地JSON支持。


1

请纠正我,但我认为as3corelib提供的API不支持流式传输。我在原问题中添加了一些示例代码以澄清。 - Kurtiss Hare
抱歉,我没有正确阅读问题。我阅读的关于as3 JSON库的最新信息在这里:http://www.darronschall.com/weblog/2008/12/as3-json-library-now-a-little-less-strict.cfm。不过无法提供太多帮助:( - George Profenza

0

你可以使用URLStream实例来逐步从远程网络下载数据,然后在有足够的数据可用时解码JSON结果。

像这样(未经测试,只是为了给你一个想法):

var stream:URLStream = new URLStream();
stream.addEventListener( ProgressEvent.PROGRESS, handleProgress );
stream.load( new URLRequest( "/path/to/data" ) );

function handleProgress( event:ProgressEvent ):void
{
    // Attempt to read as much from the stream as we can at this point in time
    while ( stream.bytesAvailable )
    {
        // Look for a JSONParseError if the JSON is not complete or not
        // encoded correctly.
        // Look for an EOFError is we can't read a UTF string completely
        // from the stream.
        try
        {
            var result:* = JSON.decode( stream.readUTF() );
            // If we're here, we were able to read at least one decoded JSON object
            // while handling this progress event
        }
        catch ( e:Error )
        {
            // Can't read, abort the while loop and try again next time we
            // get download progress.
            break;
        }
    }   
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接