1

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

mcgrailm
  • 17,469
  • 22
  • 83
  • 129

3 Answers3

1

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'
                   }
                 }";
StapleGun
  • 690
  • 7
  • 14
  • this is ultimately what i did only I reversed the quote types meaing I put single quotes on the out side and double on the inside. I then converted to json and all was good – mcgrailm Jan 20 '11 at 14:09
1

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.

Juan Pablo Califano
  • 12,213
  • 5
  • 29
  • 42
  • this is sort of the same as what StapleGun said just offers other alternative – mcgrailm Jan 20 '11 at 14:11
  • @mcgrailm. Well, sort of, but in this case `flashvars` is a JS object and `flashvars.preload` is a JSON object (a string representing an object). In StapleGun's code `flashvars` itself is a JSON string. But if both ways work, that's fine. – Juan Pablo Califano Jan 20 '11 at 22:27
0

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] );
}
PatrickS
  • 9,539
  • 2
  • 27
  • 31