2013-02-01 26 views
1

如何從Ajax請求中使用PHP獲取二級JSON節點的值? 如果我在客戶端的下一個JSON數據:如何從Ajax請求中使用PHP獲取二級JSON節點的值?

var Data = {idJS: "1", dataToSet: "example", another:{ field1: "example2"} }; 

當Ajax請求完成後,在PHP中,dataToSet是得到如下:$_POST['dataToSet'],特別是,我用笨,然後我用$this->input->post('dataToSet')。但是,如何獲得another->field1

[解決]:

$postdata = $this->input->post(); 
$postdata['another']['field1']; 

回答

0

這應做到:

$data = $this->input->post('another'); 
print_r($data->field1); 

// or you can do it in one line: 
$this->input->post('another')->field1; 
2

我不知道你是怎麼用笨做到這一點,但你必須到JSON解碼。之後,您可以訪問該字段。這可能是這個樣子:

$postdata = json_decode($this->input->post); 
$postdata['another']['field1]; 
+0

你需要通過真正的json_decode的第二個參數來檢索數組。 – m4t1t0

+0

我在Ajax請求(jQuery)中使用json類型,我不需要'json_decode'來接收數據。事實上,如果我使用該功能,數據不會被檢索到。我不知道爲什麼 – vicenrele

0

試試這個:

$ata = json_decode($this->input->post('dataToSet'), true); 
$field1 = $data['another']['field1'];