jquery - How to read JSON using javascript? -
i have json data -
{ "details": [ { "a": { "name": "mike", "age": 22 }, "b": { "name": "john", "age": 25 } } ] }
i want read a,b points array.
another way json, object.keys(),since options not in array form, can use convert array form.
var json = { "details": [ { "a": { "name": "mike", "age": 22 }, "b": { "name": "john", "age": 25 } } ] } var outputdiv = document.getelementbyid('output'); var options = object.keys(json.details[0]).map(function(item){ return '<option value="'+item+'">'+ item +'</option>' }) options.unshift('<option value="" > please select </option>') var select = document.getelementbyid('your_options'); select.innerhtml = options.join() select.onchange = function() { outputdiv.innerhtml = json.stringify(json.details[0][this.value]); }
<label>you options</label> <select id="your_options"> </select> <div id="output"></div>
Comments
Post a Comment