After successfully retrieving a JSON array from PHP using AJAX, I am now faced with the task of utilizing specific values within the array.
Although I can currently display the results as a string, my goal is to access and use individual values independently.
The JSON array I receive looks like this:
{"btn_col_preset_id":"1","btn_col_preset_title":"Pink","btn_col_preset_bg":"#ff66ef","btn_col_preset_text":"#16f3ed"}
In my JavaScript code
for (var i in myObject) {
if (myObject.hasOwnProperty(i)) {
//console.log(myObject[i]);
// alert(JSON.stringify(myObject[i]));
val1 = ???; // this is what i am trying to achieve
}
}
Update:
Within the Ajax call, I am attempting to extract a single value based on the key. However, the alerts are returning empty.
$.ajax({
type: 'POST',
url: url,
dataType: 'json',
beforeSend: function() {
},
success: function(data) {
var myObject = data;
// loop over each item
for (var i in myObject) {
if (myObject.hasOwnProperty(i)) {
//console.log(myObject[i]);
// alert(JSON.stringify(myObject[i]));
alert(myObject["btn_col_preset_id"]);
}
}
}
});