我需要用php处理它(注意发送的数据是JSON):
$.post("calculate.php", existingJsonData,
function(data) {
//alert("Data Loaded: " + data);
console.log("test data", data.values);
});
existingJsonData的格式如下:
{
"object1": {
"object11": {"x": "10", "y": "20", "z": "30"},
"object12": {"x": "40", "y": "50", "z": "60"},
"object13": {"x": "70", "y": "80", "z": "90"}
},
"object2": {
"object21": {"x": "100", "y": "200", "z": "300"},
"object22": {"x": "400", "y": "500", "z": "600"},
"object23": {"x": "700", "y": "800", "z": "900"}
}
}
php需要为“object2”中的每个x添加1。
答案 0 :(得分:1)
这个对象2中的每个x都加1,并提醒你递增的值
<?php
if ($_POST){
ob_clean();
$objects = $_POST['object2'];
foreach($objects as $a => $subObject) {
$objects[$a] = $subObject['x'] + 1;
}
die(json_encode((object) $objects));
}
?>
<script>
var existingJsonData= $.parseJSON('{ "object1": { "object11": {"x": "10", "y": "20", "z": "30"}, "object12": {"x": "40", "y": "50", "z": "60"}, "object13": {"x": "70", "y": "80", "z": "90"} }, "object2": { "object21": {"x": "100", "y": "200", "z": "300"}, "object22": {"x": "400", "y": "500", "z": "600"}, "object23": {"x": "700", "y": "800", "z": "900"}}}');
$.post("<?php echo $_SERVER['PHP_SELF']; ?>", existingJsonData,
function(data) {
var data = $.parseJSON(data);
$.each(data, function(a,b){
alert(a+' : '+b);
});
});
</script>