ajax - Parsing a JSON file using Javascript/JQUery -
i have json file:
{ "guest": { "permissions": { "webportal": [ "access", "modify" ], {…} "test/dashboard": [ "access" ] }, "users": [ "guest" ], "time_out": "3600000" }, "administrator": { "permissions": { "webportal": [ "monitoring", "access", "modify" ], {…} "test/settings": [ "access", "modify" ] }, "users": [ "admin" ], "time_out": "3600000" } }
what need take first items , drop them select box. instance:
guest, administrator , under "users" section, 'n' number of entries, needs put select this:
<select id='jsonfile'> <option value='administrator'>administrator</option> <option value='guest'>guest</option> <option value='useradmin'>useradmin</option> </select>
the balance of data in json file needs put array can access later , dump values checkboxes
.
for example: under guest: permissions: webportal - there's access , modify
. that's 2 permissions there are. guest access webportal; must have access permissions. edit, modify. it?
so i'm new stuff , need make simple possible.
oh yeah, did mention we're using knockout
, drop down has ko observable array? 1 thing @ time.
part 2 comes later.
ciao!
to , display keys of highest level objects in select element, use following code.
var optionvalues = []; var data = json.parse(jsondata); //this parses data securely object can use for(option in selection) { //goes through highest level keys of object if (data.hasownproperty(option)) { //this ignores default object properties (things don't want) if(option === 'users') //takes account user titles jsondata['users'].foreach(function(usertitle) { //iterates on of user data , appends usertitles array optionvalues.push('user'+usertitle); }); else { // default roles admin optionvalues.push(option); //this puts of non-user keys array } } }
this output => ['guest','administrator','useradmin']
data posted above. use document.createelement , document.appendchild create options of these , place them under select element in document.
Comments
Post a Comment