How to change value inside a JSON string.
Posted
by
Jeremy Roy
on Stack Overflow
See other posts from Stack Overflow
or by Jeremy Roy
Published on 2011-02-17T14:17:16Z
Indexed on
2011/02/17
15:25 UTC
Read the original article
Hit count: 246
JSON
I have a JSON string array of objects like this.
[{"id":"4","rank":"adm","title":"title 1"},
{"id":"2","rank":"mod","title":"title 2"},
{"id":"5","rank":"das","title":"title 3"},
{"id":"1","rank":"usr","title":"title 4"},
{"id":"3","rank":"ref","title":"title 5"}]
I want to change the title value of it, once the id is matching. So if my variable myID is 5, I want to change the title "title 5" to new title, and so on. And then I get the new JSON array to $("#rangArray").val(jsonStr);
Something like
$.each(jsonStr, function(k,v) {
if (v==myID) {
this.title='new title'; $("#myTextArea").val(jsonStr);
}
});
Here is the full code.
$('img.delete').click(function() {
var deltid = $(this).attr("id").split('_');
var newID = deltid[1];
var jsonStr = JSON.stringify(myArray);
$.each(jsonStr, function(k,v) {
if (v==newID) {
// how to change the title
jsonStr[k].title = 'new title';
alert(jsonStr);
$("#rangArray").val(jsonStr);
}
});
});
The above is not working. Any help please?
© Stack Overflow or respective owner