在 AS3 中解码 JSON FlashVar
我正在努力通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此页面显示 flashvars 的正确用法是:
在您的代码中,您
有我想它应该类似于
Flash 代码中的内容,您可以通过
或
root.loaderInfo.parameters.myvariable
达到此目的,或者如果您喜欢这种方式
,
则或
paramObj.myvariable
。This page shows that the correct usage of flashvars is:
In your code you have
So I guess it should be something like
then in the flash code you can reach this by
or
root.loaderInfo.parameters.myvariable
or if you prefer this way
then
or
paramObj.myvariable
.感谢您的回复..我昨天也发现了这一点,但 8 小时无法回答我自己的问题(新用户..)。我做了什么:
在嵌入 swf 的 HTML 文件中:
然后在 AS3 代码中,我使用以下方式访问 FlashVars:
然后照常处理 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:
then in the AS3 code I accessed the FlashVars with:
then processed the JSON object as usual..thanks for your help!