在 AS3 中解码 JSON FlashVar

发布于 2024-11-03 03:46:33 字数 1920 浏览 0 评论 0原文

我正在努力通过 FlashVars 将 JSON 对象传递给 .swf 并在 AS3 中解码 JSON FlashVars,希望你能帮助我。

问题: 当我尝试解码 FlashVars 时,出现 JSONParseError: Unexpected o丰富

情况:

PHP 函数从哈希创建图像路径,并将它们放在这个 JSON 对象中:

[{"index":"0", "path":"image1", "ext":"jpg"},
{"index":"1", "path":"image2", "ext":"jpg"},
{"index":"2", "path":"image3", "ext":"jpg"}]

此 JSON 通过 Flashvars 传递到 .swf。在 HTML 中,我有以下内容:

<param name="FlashVars" value='[{"index":"0", "path":"image1", "ext":"jpg"},{"index":"1", "path":"image2", "ext":"jpg"},{"index":"2", "path":"image3", "ext":"jpg"}]'/>

PHP 函数和 .swf 位于网络空间上的同一文件夹中

然后我尝试使用此 AS3 代码(导入 as3corelib)解码 main.as 文件中的 FlashVars:

var imagePaths:Object;    
try {

    var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
    trace(paramObj.toString());
    if(paramObj){
        imagePaths = JSON.decode(paramObj.toString());
    }
}
catch (error:Error)
        {
            trace(error.toString());
        }

“imagePaths”包含JSON 对象,以便稍后可以构建图像的路径。据我了解 JSON.decode 返回一个 当我从单独的 txt 文件在 localhost 上加载完全相同的 JSON 时,效果很好:

var imagePathLoader:URLLoader = URLLoader(e.target);
imagePaths = JSON.decode(imagePathLoader.data);

错误显然发生在 try 块中,因此我使用 trace( 跟踪 paramObj 变量paramObj.toString()); 并获取[object Object]作为输出。 在我看来,JSON 解码函数正在尝试解码字符串 [object Object] 而不是对象本身,因此会在“object”的第一个“o”处抛出错误?

在作为 FlashVars 传递之前,我已经尝试对 PHP 中的 JSON 进行 urlencode(),在 http://code.google.com/p/as3corelib/issues/detail?id=119 并执行了 JSON.decode(paramObj); 没有.toString() 抛出此错误:

1118:值的隐式强制转换 与静态类型对象可能 不相关的类型字符串。

那么,如何将 JSON 正确传递给 .swf 并在 AS3 中将其解码为对象?

预先感谢您的任何帮助

I'm struggling with passing a JSON object to a .swf via FlashVars and decoding the JSON FlashVars in AS3 and was hoping you could help me.

Problem:
I get an JSONParseError: Unexpected o encountered when I try to decode the FlashVars.

Situation:

A PHP function creates paths to images from a hash and puts them together in this JSON object:

[{"index":"0", "path":"image1", "ext":"jpg"},
{"index":"1", "path":"image2", "ext":"jpg"},
{"index":"2", "path":"image3", "ext":"jpg"}]

This JSON is passed to .swf via Flashvars. In the HTML I have this:

<param name="FlashVars" value='[{"index":"0", "path":"image1", "ext":"jpg"},{"index":"1", "path":"image2", "ext":"jpg"},{"index":"2", "path":"image3", "ext":"jpg"}]'/>

The PHP function and the .swf are in the same folder on the webspace

Then I try to decode the FlashVars in my main.as file with this AS3 Code (as3corelib is imported):

var imagePaths:Object;    
try {

    var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
    trace(paramObj.toString());
    if(paramObj){
        imagePaths = JSON.decode(paramObj.toString());
    }
}
catch (error:Error)
        {
            trace(error.toString());
        }

"imagePaths" holds the JSON object so later the paths to the images could be constructed. As far as I understood JSON.decode returns an This worked fine when I loaded the exactly same JSON on localhost from a separate txt file with:

var imagePathLoader:URLLoader = URLLoader(e.target);
imagePaths = JSON.decode(imagePathLoader.data);

The error obviously occurs in the try block, so I traced the paramObj variable with trace(paramObj.toString()); and get [object Object]as output.
It seems to me that the JSON decode function is trying to decode the string [object Object] rather than the object itself and therefore throws an error at the first "o" of "object"?

I already tried to urlencode() the JSON from PHP before passing as FlashVars, the suggestions found on http://code.google.com/p/as3corelib/issues/detail?id=119 and did JSON.decode(paramObj); without .toString() which throws this error:

1118: Implicit coercion of a value
with static type Object to a possibly
unrelated type String.

So, how do I pass the JSON correctly to the .swf and decode it in AS3 to an object?

Thanks in advance for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2024-11-10 03:46:33

此页面显示 flashvars 的正确用法是:

<PARAM NAME=FlashVars VALUE="varname=value">

在您的代码中,您

<param name="FlashVars" value='[{"index":"0", "path":"image1", "ext":"jpg"},{"index":"1", "path":"image2", "ext":"jpg"},{"index":"2", "path":"image3", "ext":"jpg"}]'/>

有我想它应该类似于

<param name="FlashVars" value='myvariable=[{"index":"0", "path":"image1", "ext":"jpg"},{"index":"1", "path":"image2", "ext":"jpg"},{"index":"2", "path":"image3", "ext":"jpg"}]'/>

Flash 代码中的内容,您可以通过

root.loaderInfo.parameters["myvariable"]

root.loaderInfo.parameters.myvariable

达到此目的,或者如果您喜欢这种方式

var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;

paramObj["myvariable"]

则或

paramObj.myvariable

This page shows that the correct usage of flashvars is:

<PARAM NAME=FlashVars VALUE="varname=value">

In your code you have

<param name="FlashVars" value='[{"index":"0", "path":"image1", "ext":"jpg"},{"index":"1", "path":"image2", "ext":"jpg"},{"index":"2", "path":"image3", "ext":"jpg"}]'/>

So I guess it should be something like

<param name="FlashVars" value='myvariable=[{"index":"0", "path":"image1", "ext":"jpg"},{"index":"1", "path":"image2", "ext":"jpg"},{"index":"2", "path":"image3", "ext":"jpg"}]'/>

then in the flash code you can reach this by

root.loaderInfo.parameters["myvariable"]

or

root.loaderInfo.parameters.myvariable

or if you prefer this way

var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;

then

paramObj["myvariable"]

or

paramObj.myvariable.

独木成林 2024-11-10 03:46:33

感谢您的回复..我昨天也发现了这一点,但 8 小时无法回答我自己的问题(新用户..)。我做了什么:
在嵌入 swf 的 HTML 文件中:

<param name="FlashVars" value="var=urlencode([{"index":"0", "path":"image1", "ext":"jpg"},
{"index":"1", "path":"image2", "ext":"jpg"},
{"index":"2", "path":"image3", "ext":"jpg"}])"/>

然后在 AS3 代码中,我使用以下方式访问 FlashVars:

var imagePaths:Object;    
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
var myFlashVar:String;
var varName:String;
for (varName in paramObj) {
    myFlashVar = String(paramObj[varName]);
}
imagePaths = JSON.decode(myFlashVar);

然后照常处理 JSON 对象..感谢您的帮助!

Thanks for the replies..I also found this out yesterday but couldn't answer my own question for 8h (new user..). What I did:
In the HTML file that embeds the swf:

<param name="FlashVars" value="var=urlencode([{"index":"0", "path":"image1", "ext":"jpg"},
{"index":"1", "path":"image2", "ext":"jpg"},
{"index":"2", "path":"image3", "ext":"jpg"}])"/>

then in the AS3 code I accessed the FlashVars with:

var imagePaths:Object;    
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
var myFlashVar:String;
var varName:String;
for (varName in paramObj) {
    myFlashVar = String(paramObj[varName]);
}
imagePaths = JSON.decode(myFlashVar);

then processed the JSON object as usual..thanks for your help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文