</div>
</div>
<div class="grid--cell mb0 mt4">
<a href="/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json" dir="ltr">How can I access and process nested objects, arrays or JSON?</a>
<span class="question-originals-answer-count">
(25 answers)
</span>
</div>
<div class="grid--cell mb0 mt8">Closed <span title="2016-05-10 15:00:38Z" class="relativetime">3 years ago</span>.</div>
</div>
</aside>
i have created a dynamic array and then, i convert it to json object. The code is the follows:
$array_D1[]="";
$array_D2[]="";
$array_to_send[]="";
$array_D1[$CapaEnviar][] = $fila['test_name'];
$array_D2[]=$seleccio_content;
$array_D2[]=$id_capa;
$array_D2[]=$pagina;
$array_D2[]=$idtest;
$array_D2[]=$valor_org;
$array_D2[]=$valor_eval;
$array_D2[]=$valor_test;
$array_to_send=array('data1' => $array_D1, 'data2' => $array_D2);
echo json_encode($array_to_send,true);
This code is a AJAX response for an AJAX request. I don't know how i can access JSON data from jquery.
i need to acces the values from "data1" and "data2"
</div>
I'm not sure what's hard about this. The others are right that you probably should add:
header('Content-type: application/json');
to your php file before your echo statement.
Assuming that you can access the php with a url, say, http://blah.com/test.php, you can...
$.ajax({
method: 'post', //or 'get'
url: 'http://blah.com/test.php'
})
.done(function(data) {
console.dir(data.data1);
console.dir(data.data2);
})
.fail(function(jqXHR, textStatus) {
console.dir(jqXHR);
console.dir(textStatus);
});
Obviously, you can replace the body of the callback with whatever processing you want.