JSON element detection

Posted by user3614570 on Stack Overflow See other posts from Stack Overflow or by user3614570
Published on 2014-05-27T03:17:43Z Indexed on 2014/05/27 3:27 UTC
Read the original article Hit count: 98

Filed under:
|

I’ve created a string…

{"atts": [{"name": "wedw"}, {"type": "---"}]}

I pile a bunch of these together in an array based on user input and attach them to another string to complete a JSON object that tests out as valid.

So I end up with a global array called fields with a bunch of these little snippets.

So how do I change the name "weds" with a new name? I’ve tried...

function changefieldname(pos){
var obj = JSON.parse(jsonstring);
var oldname = obj.tracelog.fields[pos].atts[0]["name"];
var newname = document.getElementById("newlogfieldname"+pos).value;
fields[pos].replace(oldname, newname);
//writejson();
}

And a bunch of variations. I know everything is checking out correct interms of the variables pos, oldname, and newname. I also know that fields[pos] returns the string in the array I want to correct but it’s not happy. I also tried converting fields[pos] to a string, but the replace function doesn't work on it. I’m sure there is a good reason.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON