Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中将json对象传递到javascript函数_Javascript_Php_Json - Fatal编程技术网

如何在php中将json对象传递到javascript函数

如何在php中将json对象传递到javascript函数,javascript,php,json,Javascript,Php,Json,我有一个json对象。 $json=json_decode($data,true) 看起来像- array(5) { ["screenShareCode"]=> string(9) "021113322" ["appletHtml"]=> string(668) "" ["presenterParams"]=> string(396) "aUsEN5gBYi4vlIEGpk0=" ["viewerUrl"]=> string(65) "

我有一个json对象。
$json=json_decode($data,true)
看起来像-

array(5) {
  ["screenShareCode"]=>
  string(9) "021113322"
  ["appletHtml"]=>
  string(668) ""
  ["presenterParams"]=>
  string(396) "aUsEN5gBYi4vlIEGpk0="
  ["viewerUrl"]=>
  string(65) "http://api.leap.com/v2/viewer/021113322?accountid=mynet"
  ["origin"]=>
  string(3) "API"
}

警报(“?php echo$json;?>”);
当我试图将其赋给javascript变量时,它会给我一个错误,说“unterminated string constant”。

您不能用PHP或javascript对其进行解码。JSON字符串的内容是有效的JavaScript文本

alert(<?php echo json_encode($json); ?>);

var数据=;
警报(数据[“屏幕共享代码]);
尝试:

alert();
或:

alert();

你对术语有点困惑
$data
是一个JSON字符串,
$JSON
是通过解码JSON字符串获得的PHP数组。

请尝试以下代码。我创建了一个名为sample.php的php文件。我们有一个名为$data的php数组。使用
json\u endode()
将该数据编码为json。这将生成json格式的数据。然后我们可以将其分配到java脚本变量中,如
var jsonData=请注意,这是在
标记内完成的


//将该json数据分配给java脚本变量
var jsonData=;
//查看完整数据的步骤
console.log(jsonData);
//您可以像这样获取特定的数据。
警报(jsonData.presenterParams);

警报(“?php echo$data;?>”)
解码
$json
变量。您需要json编码的值.alert()具有“完全像
var obj=json.parse()一样尝试”的属性
<?php
 ...
?>
<script ...>
var data=<?php echo $data; ?>;
alert(data["screenShareCode"]);
<?php
 ...
alert(<?php echo json_encode($json); ?>);
alert(<?php echo $data; ?>);
<?php
// We have some data
$data = array(
    "screenShareCode"=>"021113322",
    "appletHtml"=>"",
    "presenterParams"=>"aUsEN5gBYi4vlIEGpk0",
    "viewerUrl"=>"http://api.screenleap.com/v2/viewer/021113322?accountid=mynet",
    "origin"=>"API"
);
// Convert it into json format.
$jsonData = json_encode($data,true);
?>

<script>
// Assign that json data to a java-script variable
var jsonData = <?php echo $jsonData ?>;

// To view the full data 
console.log(jsonData);

// You can take a specific data like this.
alert(jsonData.presenterParams);
</script>