无法解析Air中的本地JSON

时间:2013-11-25 09:23:47

标签: json actionscript-3 air utf

我有一个用于spritesheet的json,由adobe flash CS6 spritesheet生成器生成。当我尝试阅读时,我得到这个字符串:“ÿþ{”

JSON看起来像这样:

 {"frames": {

    "buttons instance 10000":
    {
        "frame": {"x":0,"y":0,"w":60,"h":48},
        "rotated": false,
        "trimmed": false,
        "spriteSourceSize": {"x":0,"y":0,"w":60,"h":48},
        "sourceSize": {"w":60,"h":48}
    },
    "buttons instance 10001":
    {
        "frame": {"x":60,"y":0,"w":60,"h":48},
        "rotated": false,
        "trimmed": false,
        "spriteSourceSize": {"x":0,"y":0,"w":60,"h":48},
        "sourceSize": {"w":60,"h":48}
    },
    "buttons instance 10002":
    {
        "frame": {"x":120,"y":0,"w":60,"h":48},
        "rotated": false,
        "trimmed": false,
        "spriteSourceSize": {"x":0,"y":0,"w":60,"h":48},
        "sourceSize": {"w":60,"h":48}
    },
    "buttons instance 10003":
    {
        "frame": {"x":180,"y":0,"w":60,"h":48},
        "rotated": false,
        "trimmed": false,
        "spriteSourceSize": {"x":0,"y":0,"w":60,"h":48},
        "sourceSize": {"w":60,"h":48}
    }},
    "meta": {
    "app": "Adobe Flash CS6",
    "version": "12.0.0.481",
    "image": "panel_animations.png",
    "format": "RGBA8888",
    "size": {"w":1024,"h":2048},
    "scale": "1"
}
}

这是我尝试阅读文件的方式:

protected function load(event:MouseEvent):void
{
    file = new File();
    file.addEventListener(Event.SELECT, file_select);
    file.browseForOpen("Please select a file...", [new FileFilter("JSON", "*.json")]);
}

protected function file_select(event:Event):void
{

     selectedFile = event.currentTarget as File;
    stream = new FileStream();
    //stream.open(file, FileMode.READ);
    stream.addEventListener(Event.COMPLETE, doTrace);
    stream.openAsync(selectedFile, FileMode.READ);
        }

    private function doTrace(e:Event):void 
{
    trace(stream.readUTFBytes(stream.bytesAvailable));
}

最后,跟踪结果为“ÿþ{”,stream.bytesAvailable = 0。如果我尝试解析它,当然我得到错误#1132:无效的JSON解析输入。

感谢任何帮助。

moks

3 个答案:

答案 0 :(得分:0)

请尝试使用readMultiByte()

private function doTrace(e:Event):void 
{
    trace(stream.readMultiByte(stream.bytesAvailable, File.systemCharset));
    stream.close();
}

答案 1 :(得分:0)

看起来问题出在flash cs6生成的json文件中。

我创建了一个新的json文件,并从生成的json文件中复制/粘贴了内容。 它有效!!

我不知道为什么这很难发生..

答案 2 :(得分:0)

这些是字节顺序字节。在这种情况下,你应该使用ByteArray的toString函数。

var obj:Object = JSON.parse(bytearray.toString());