i am learning on how to parse json using jquery
PHP
foreach($cars as $car)
{
$output['carID'][] = $car->carID;
$output['carName'][] = $car->carName;
}
echo jsonEncode($output);
JSON
{"carID":["1","2"],"carName":["BMW","Mercedez Benz"]}
My javascript
$.getJSON('/cars.php', { carID: carID }, function(data) {
$.each(data, function(i,item) {
for(j=0; j < item[item.carName]; j++) {
console.log(item[item.carName][j]);
}
});
});
I think my code is entirely wrong because its not working.
how can i get the value inside of ["1","2"] ?
Thanks for the help.
It’s not the “value of 1” that you’re looking for. It’s the value at index 0, which happens to be “1”.
["1","2"]is the value ofcarID, and it is anarray.Try accessing it this way: