CyberJunkie CyberJunkie - 3 months ago 25
jQuery Question

jQuery Ajax: get specific object value

I'm accessing a dictionary REST API using Ajax.

url: "",
dataType : 'json',
success: function(data) {
//called when successful
error: function(e) {
//called when there is an error

The response:

"textProns": [],
"sourceDictionary": "ahd-legacy",
"exampleUses": [],
"relatedWords": [],
"labels": [],
"citations": [],
"word": "cat",
"text": "A small carnivorous mammal (Felis catus or F. domesticus) domesticated since early times as a catcher of rats and mice and as a pet and existing in several distinctive breeds and varieties.",
"sequence": "0",
"score": 0.0,
"partOfSpeech": "noun",
"attributionText": "from The American Heritage® Dictionary of the English Language, 4th Edition"

In my test example I get an alert
on success. Most Ajax examples I saw use loops but I'm returning one result. How can I extract the
values from the objects?

dda dda

As you can see your response starts with [ and ends with ]. So your response is an array. Then, inside the array, you get objects (starting with { and ending with }). So your data is an array, which can be accessed with data[x] (x being the index), and each member of the selected object can be accessed with the .dot notation: .word, .text etc. So in your case, if there's only one result, you can do data[0].word.