2011-10-24 40 views
0

我需要用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需要1添加到每個的x在「object2」中。

回答

1

這一次在對象2,並提醒您加1到每個X的的增加值

<?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> 

demo

+0

感謝。如果PHP文件包含如下內容---> echo json_encode($ objects); –

+0

@JohnR:添加了整個代碼 – genesis

+0

@JohnR:我花了很多時間來測試這個,希望你喜歡它 – genesis