当前位置: 动力学知识库 > 问答 > 编程问答 >

json - php json_encode + modernizr object

问题描述:

I'm looking for better and more robust solution for echoing out yepnope feature tests using php. The output should look something like :

{

test : Modernizr.geolocation,

yep : 'normal.js',

nope : ['polyfill.js', 'wrapper.js']

}

From an output like:

$l10n = array(

'test' => 'Modernizr.geolocation',

'yep' => "'normal.js'",

'nope' => array("'polyfill.js'", "'wrapper.js'")

);

Obviously, there is the issue of quotation marks being wrapped around the json object. I can't help but wonder if there's a different class altogether that caters to creating mixed javascript objects containing raw javascript as well as strings.

网友答案:

json_encode returns the JSON representation of a value, the point is JSON representation is not a javascript object, JSON is a subset of the javascript object literal, so you need to do the convert in javascript.

var l10n = <?php echo json_encode($l10n); ?>;

if (l10n.test === "Modernizr.geolocation") {
  l10n.test = Modernizr.geolocation;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: