So hi guys,
所以你们好,
lets make this very clear. In php i used json_encode(...) and then got the value in javascript, looks as the following:
让我们说清楚。在php中,我使用json_encode(…),然后在javascript中获取值,如下所示:
["float","float","float","float"] // PS: This is a string...
And i would like to make this into a normal javascript array, like so:
我想把它做成一个普通的javascript数组,如下所示:
Arr[0] // Will be float
Arr[1] // Will be float
Arr[2] // Will be float
Arr[3] // Will be float
And now I'm asking you, how is this possible?
现在我问你,这怎么可能?
Thank You
谢谢你!
3 个解决方案
#1
17
It sounds like you're retrieving a JSON string in JavaScript (perhaps via AJAX?). If you need to make this into an actual array value, you'd probably want to use JSON.parse()
.
听起来您正在用JavaScript(也许是通过AJAX)检索JSON字符串。如果需要将其转换为实际的数组值,您可能需要使用JSON.parse()。
var retrievedJSON = '["float","float","float","float"]'; // normally from AJAX
var myArray = JSON.parse(retrievedJSON);
If you're actually writing out a value into the page, rather than using AJAX, then you should be able to simply echo the output of json_encode
directly, without quoting; JSON itself is valid JavaScript.
如果您实际上是在页面中写入一个值,而不是使用AJAX,那么您应该能够直接响应json_encode的输出,而不需要引用;JSON本身就是有效的JavaScript。
var myArray = <?php echo json_encode($myPhpArray); ?>;
#2
7
var myArray = <?= json_encode($myPhpArray); ?>;
Pretty simple. ;-)
很简单。:-)
Example:
例子:
<?php
$myPhpArray = array('foo', 'bar', 'baz');
?>
<script type="text/javascript">
var myJsArray = <?= json_encode($myPhpArray); ?>;
</script>
Should output (view-source):
应该输出(查看源代码):
<script type="javascript">
var myJsArray = ["foo","bar","baz"];
</script>
例子
#3
2
I reccomend using jquery. The php file should look as such ...
我推荐使用jquery。php文件应该是这样的……
//location.php
<?php
$change = array('key1' => $var1, 'key2' => $var2, 'key3' => $var3);
echo json_encode($change);
?>
Then the jquery script ...
然后jquery脚本…
<script>
$.get("location.php", function(data){
var duce = jQuery.parseJSON(data);
var art1 = duce.key1;
var art2 = duce.key2;
var art3 = duce.key3;
});
</script>
#1
17
It sounds like you're retrieving a JSON string in JavaScript (perhaps via AJAX?). If you need to make this into an actual array value, you'd probably want to use JSON.parse()
.
听起来您正在用JavaScript(也许是通过AJAX)检索JSON字符串。如果需要将其转换为实际的数组值,您可能需要使用JSON.parse()。
var retrievedJSON = '["float","float","float","float"]'; // normally from AJAX
var myArray = JSON.parse(retrievedJSON);
If you're actually writing out a value into the page, rather than using AJAX, then you should be able to simply echo the output of json_encode
directly, without quoting; JSON itself is valid JavaScript.
如果您实际上是在页面中写入一个值,而不是使用AJAX,那么您应该能够直接响应json_encode的输出,而不需要引用;JSON本身就是有效的JavaScript。
var myArray = <?php echo json_encode($myPhpArray); ?>;
#2
7
var myArray = <?= json_encode($myPhpArray); ?>;
Pretty simple. ;-)
很简单。:-)
Example:
例子:
<?php
$myPhpArray = array('foo', 'bar', 'baz');
?>
<script type="text/javascript">
var myJsArray = <?= json_encode($myPhpArray); ?>;
</script>
Should output (view-source):
应该输出(查看源代码):
<script type="javascript">
var myJsArray = ["foo","bar","baz"];
</script>
例子
#3
2
I reccomend using jquery. The php file should look as such ...
我推荐使用jquery。php文件应该是这样的……
//location.php
<?php
$change = array('key1' => $var1, 'key2' => $var2, 'key3' => $var3);
echo json_encode($change);
?>
Then the jquery script ...
然后jquery脚本…
<script>
$.get("location.php", function(data){
var duce = jQuery.parseJSON(data);
var art1 = duce.key1;
var art2 = duce.key2;
var art3 = duce.key3;
});
</script>