How to read JSON using javascript?

Home / Uncategorized / How to read JSON using javascript?

Question:
I have JSON data like this –

{
"details": [
{
"A": {
"Name": "mike",
"Age": 22
},
"B": {
"Name": "John",
"Age": 25
}
}
]
}

I want to read A,B points as an array.


Answer:
Another way to do it with your json, Object.keys(),since your options are not in array form, can use that to convert to 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 ‘
})
options.unshift(‘‘)
var select = document.getElementById(‘your_options’);
select.innerHTML = options.join()
select.onchange = function() {
outputDiv.innerHTML = JSON.stringify(json.details[0][this.value]);
}

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *