Despite finding numerous similar posts on this topic, none of the solutions I have attempted seem to be effective. My question is straightforward and should not be difficult to answer. The JSON object I am receiving will have only one key and one value. In the code snippet below, the alert displays "[{"DISCOUNT":1}]", and all I want to do is extract the "1" and show it independently. My ultimate goal is to assign that value to a variable for multiplication purposes, but extracting the number alone has proven challenging. Here is the code:
function codeMatchTest() {
if ($('#dbReturnString').val() == '') {
alert("Please enter a discount code.");
} else {
$.ajax({
type: "POST",
url: "PROMO.svc/MatchCode",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({ codeInput: $('#dbReturnString').val().toLowerCase() }),
success: function (json) {
alert(json.d);
/*alert (json.d.discount); // getting "undefined"
$.each(json.d, function (key, value) {
var discount = value;
});
alert("Success: " + discount); //getting "undefined" */
},
error: function () {
alert("There was an error with your request.");
}
});
}
}
I have been unable to find useful resources on properly handling data in a JSON object. My JSON object will consistently contain just a single key and value, with the necessity of only utilizing the value.
I have attempted multiple iterations using $.each without success. Despite the simplicity suggested by the jQuery documentation, I have had no luck.