flashvars 中的 json
我想在我的 flashvars 中使用 josn,但我遇到了麻烦
,这是我的 json
var flashvars = {
xmlFile: '<?php echo $preface.$xmlpath; ?>',
preface: '<?php echo $preface; ?>',
"preload": {
"url": "flash/someflash.swf",
"x": "375",
"y": "237"
}
};
这是我一直在尝试的
var jsondata:String = this.loaderInfo.parameters.preload;
if(jsondata){
//var jsonData:Object = JSON.decode(jsondata.toString()) ;
error_txt.text = jsondata.toString();
}
error_txt 返回“object Object”,但我无法访问 json 对象的任何部分
I want to use josn in my flashvars and I'm having trouble
here is my json
var flashvars = {
xmlFile: '<?php echo $preface.$xmlpath; ?>',
preface: '<?php echo $preface; ?>',
"preload": {
"url": "flash/someflash.swf",
"x": "375",
"y": "237"
}
};
here is what I have been trying
var jsondata:String = this.loaderInfo.parameters.preload;
if(jsondata){
//var jsonData:Object = JSON.decode(jsondata.toString()) ;
error_txt.text = jsondata.toString();
}
error_txt returns "object Object" but I can't access any part of the json object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信这里发生的事情是 Javascript 在 flashvars 变量上调用 .toString() 并将结果字符串传递给 flash。如果我的预感是正确的,您将需要将 JSON 作为字符串传递,如下所示。
I believe what is happening here is that Javascript calls .toString() on your flashvars variable and passes the resulting string to flash. If my hunch is correct you will need to pass the JSON as a string such as this.
Flashvars 作为名称/值对的集合传递,其格式与 GET 或 POST(url 编码)参数相同。因此,对于名称和值,您需要内容是一个字符串,并正确转义。除了硬编码 JSON 字符串(这很容易出错)之外,您还可以将数据写入 php assoc 数组,然后将其编码为 JSON,然后对其进行 url 编码。生成的字符串就是您将作为值传递的内容。
像这样的东西(我实际上没有测试过这个片段!)
PS
再想一想,SWFObject(您似乎用来嵌入 swf 的)很可能会为您进行 url 转义(通过encodeURIComponent或一些自制函数);我不记得是否是这种情况,但如果是这样,您不必在 php 代码中调用
rawurlencode
,因为您的数据将被 urlencoded 两次。我现在无法测试这个,但是可以在 php 中使用和不使用 url 编码来尝试一下;两者之一应该可以正常工作。Flashvars are passed as a collection of name/value pairs, with the same format as GET or POST (url-encoded) parameters. So for both the name and the value you need the content to be a string, properly escaped. Other than hardcoding the JSON string, which is a bit error prone, you could write your data in a php assoc array, then encode it to JSON and then url encode it. The resulting string is what you will pass as the value.
Something like this (I haven't actually tested this snippet!)
PS
On second thoughts, it's quite likely that the SWFObject (which you seem to be using to embed the swf) does the url escaping for you (via encodeURIComponent or some home made function); I don't remember if that's the case, but if it is, you don't have to call
rawurlencode
in your php code, as your data will get urlencoded twice. I can't test this right now, but give it a try with and without url-encoding in php; one of the two should work fine.你实际上在哪里使用/需要 JSON?...
Where do you actually use/need JSON?...