I have a json string like
{
"Msg1": "message 1",
"Msg2": "message 3",
"Msg3": "message 2"
}
I am using the folowing code
function GetMessages(msg) {
$.getJSON("./jquery/sample.json", function (result) {
$.each(result, function (key, val) {
if (key == msg) {
alert(val);
}
});
}
Is there any other way to check if my key exists in the result array & get its value without using a foreach loop ? Can eval() do something ?
If you know the property name you could access it directly and you don’t need to be looping through its properties: