flashvars 中的 json

发布于 2024-10-12 19:25:21 字数 705 浏览 4 评论 0原文

我想在我的 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 技术交流群。

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

发布评论

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

评论(3

指尖凝香 2024-10-19 19:25:21

我相信这里发生的事情是 Javascript 在 flashvars 变量上调用 .toString() 并将结果字符串传递给 flash。如果我的预感是正确的,您将需要将 JSON 作为字符串传递,如下所示。

var flashvars = "{xmlFile:'myFile.xml',
                  preface:'Preface',
                   {
                    'url': 'flash/someflash.swf',
                    'x': '375',
                    'y': '237'
                   }
                 }";

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.

var flashvars = "{xmlFile:'myFile.xml',
                  preface:'Preface',
                   {
                    'url': 'flash/someflash.swf',
                    'x': '375',
                    'y': '237'
                   }
                 }";
我一直都在从未离去 2024-10-19 19:25:21

Flashvars 作为名称/值对的集合传递,其格式与 GET 或 POST(url 编码)参数相同。因此,对于名称和值,您需要内容是一个字符串,并正确转义。除了硬编码 JSON 字符串(这很容易出错)之外,您还可以将数据写入 php assoc 数组,然后将其编码为 JSON,然后对其进行 url 编码。生成的字符串就是您将作为值传递的内容。

像这样的东西(我实际上没有测试过这个片段!)

<?php
$preload_data = array(
     "url"      => "flash/someflash.swf",
        "x"     => "375",
        "y"     => "237"
); 
$preload_flashvar = rawurlencode(json_encode($preload_data));
?>

var flashvars = { 
                xmlFile: '<?php echo $preface.$xmlpath; ?>',
                preface: '<?php echo $preface; ?>',
                preload: '<?php echo $preload_flashvar; ?>'
                };

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!)

<?php
$preload_data = array(
     "url"      => "flash/someflash.swf",
        "x"     => "375",
        "y"     => "237"
); 
$preload_flashvar = rawurlencode(json_encode($preload_data));
?>

var flashvars = { 
                xmlFile: '<?php echo $preface.$xmlpath; ?>',
                preface: '<?php echo $preface; ?>',
                preload: '<?php echo $preload_flashvar; ?>'
                };

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.

§对你不离不弃 2024-10-19 19:25:21

你实际上在哪里使用/需要 JSON?...

var flashvars = { 
            xmlFile: '<?php echo $preface.$xmlpath; ?>',
            preface: '<?php echo $preface; ?>',
            preload: {
                       url: "flash/someflash.swf",
                       x: "375",
                       y: "237"
                      }
            };


//in AS3
var params:Object  = this.loaderInfo.parameters;

if(params != null)
{
  var preload:Object = params.preload;

  for( var name:String in preload )
      trace( preload[name] );
}

Where do you actually use/need JSON?...

var flashvars = { 
            xmlFile: '<?php echo $preface.$xmlpath; ?>',
            preface: '<?php echo $preface; ?>',
            preload: {
                       url: "flash/someflash.swf",
                       x: "375",
                       y: "237"
                      }
            };


//in AS3
var params:Object  = this.loaderInfo.parameters;

if(params != null)
{
  var preload:Object = params.preload;

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