First of all, your JSON isn't valid. You're missing commas and quotes.
{
"listOfVal": [
val1,
val2,
val3
],
"name": "app",
"id": "9209629",
...
}
once you have data
, iterate over it with a for
loop. You could use indexOf
, but that doesn't work in IE < 9
function isInJSON(data, value){
for(var i=0; i<data.listOfVal.length; i++){
if(data.listOfVal[i] === value){
return true;
}
}
return false;
}
This won't always work if your val
s are arrays or objects. If they are, you could run for
loops to compare them, or you could use underscore.js
: _.isEqual(obj1,obj2)
.
OP requested using forEach
:
var value; //define what you're looking for
data.listOfVal.forEach(function(val,key){
if(val === value){
alert('MATCH FOUND: responseBody["' + String(key) + '"] = ' + String(value));
}
});
,
symbols off, if he is successfully able to parse it, it's irrelevant to the question itself. Thoseval#
s should be in quotes though, as far as I can tell.