javascript - How to make a JSON skeleton and fill it in later -
i trying make json store information users. example, in end want like:
{ "people": [ { "name": "foo", "company": "bar", "email": "foo@bar.com" }, { "name": "bar", "company": "foo", "email": "bar@foo.com" } ] }
this populated through form, want able declare empty json , add on people submit form. there way declare empty json skeleton (name, company, email)? how add on entry? there sort of .append("name": "bar", "company": "foo", "email": "bar@foo.com") ? want use straight javascript, using jquery alright if needed.
with javascript, there no need predefine in json object.
this valid code:
var o = {}; // create new object o.name = "bob"; console.log(o.name); // prints bob o.scores = new array(); o.scores[o.scores.length] = 1; o.scores[o.scores.length] = 2; o.scores[o.scores.length] = 3; console.log(o.scores); // prints [1, 2, 3] o.info = {}; o.info.isset = true; console.log(o.info.isset); // prints true console.log(o.info.isnotset); // prints undefined
so can dynamically build whatever type of json need form.
if want render json string, can use json.stringify(o)
, , print:
{"name":"bob","scores":[1,2,3],"info":{"isset":true}}
Comments
Post a Comment