How do i add new attribute (element) to JSON object using javascript
+16
A:
JSON stands for JavaScript Object Notation. A JSON object is really a string that has yet to be turned into the object it represents.
To add a property to an existing object in JS you could do the following.
object["property"] = value;
or
object.property = value;
If you provide some extra info like exactly what you need to do in context you might get a more tailored answer.
Quintin Robinson
2009-04-10 03:03:54
+3
A:
A JSON object is simply a javascript object, so with Javascript being a prototype based language, all you have to do is address it using the dot notation.
mything.NewField = 'foo';
FlySwat
2009-04-10 03:04:59
The courtesy here is to accept an answer.
Crescent Fresh
2009-04-10 18:04:43
A:
^^FlySwat "mything.NewField = 'foo';"
how to modify your example if I want NewField to be variable? I mean the new property will be the value of the var NewField ?
Mukul Chaudhuri
2010-09-28 14:33:40