Json replacing values in a loop
I've a large JSON string describing some data. I'd like to loop through this and replace value of all properties named "Key". How can I achi开发者_StackOverflow社区eve this using jQuery or plain Javascript?
var item = $.parseJson("jsonstring...");
var target = "Key";
for (var k in item) {
if (item.hasOwnProperty(k) && item[k].hasOwnProperty(target)) {
item[k][target] = "replacement value"
}
}
Or similar, depending on the structure of your JSON. This is assuming it's a list of objects.
for ( var i in data ) {
for ( var k in data[i] ) {
if ( k == 'Key' ) {
data[i][k] = 'new value';
}
}
}
for (property in setJson)
{ if (property != 'Key') //do some think }
精彩评论