Outputting key from a JSON file
Hi,
If this is the JSON:
Code:
[
{
"question": "What's a Dog?",
"answer": "The domestic dog is a member of genus Canis that forms part of the wolf-like canids, and is the most widely abundant carnivore."
},
{
"question": "What’s an Elephant?",
"answer": "Elephants are large mammals of the family Elephantidae and the order Proboscidea. Two species are traditionally recognised, the African elephant and the Asian elephant, although some evidence suggests that African bush elephants and African forest elephants are separate species"
},
{
"question": "What's a Tiger ?",
"answer": "The tiger is the largest cat species, most recognisable for their pattern of dark vertical stripes on reddish-orange fur with a lighter underside. The species is classified in the genus Panthera with the lion, leopard, jaguar and snow leopard. Tigers are apex predators, primarily preying on ungulates such as deer and bovids."
}
]
I can access the value of each question and answer with this loop:
Code:
for (var i in data) {
result += '<div>' + data[i].question + '</div>';
result += '<div>' + data[i].answer + '</div>';
}
But how can I output the key of each property, which would be the strings "question" and "answer"?
PS: The purpose of this question is, to learn and understand how to have full control over outputting and working with a JSON file.
Thanks