第一个没有“sodas”键的方括号的语句会起作用。我不确定您使用的是哪种语言,但这里是 HTML、jQuery 和 PHP 的示例。
HTML(文件:y.html)
<!DOCTYPE html>
<html>
<head>
<title>XYZ</title>
<script
type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<input type="button" id="send" value="Send">
<hr noshade>
<div id="output"></div>
<script type="text/javascript">
var $output = $('#output');
$('#send').click(function(e){
e.preventDefault();
var $json = '{"sodas":["coke","pepsi","fanta"]}';
$.ajax({url:"/so/y.php",type:"post",dataType:"html",data:'json='+escape($json),
success:function(obj){
$output.html(obj);
}
});
});
</script>
</body>
</html>
JavaScript escape() 函数将 json POST 参数格式化如下(取自 Firebug。)
json=%7B%22sodas%22%3A%5B%22coke%22%2C%22pepsi%22%2C%22fanta%22%5D%7D
PHP(文件:y.php)
<?php
$json = json_decode(stripslashes($_POST['json']));
var_dump($json);
The browser output displays the var_dump()'d string representation of a PHP object, single-keyed associative array with the value being an array of three soda brands.
object(stdClass)#1 (1) { ["sodas"]=> array(3) { [0]=> string(4) "coke" [1]=> string(5) "pepsi" [2]=> string(5) "fanta" } }