Wikipedia API - access to a JSON object

I am trying to get text from a Wikipedia article using their API (the API is a generous term for what they offer, but we will use it, I think), and I am having problems parsing the subsequent JSON object I return. Namely, the object contains the text that I am looking for under the key, whose label is "*", so after executing the command:

$.getJSON("http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {page:"Red Sea clownfish", prop:"text"}, function(data) { 

Then I try to parse this information in a line using the command:

 var dat = data.parse.text.*; 

Then I output to the console using:

 console.log(dat); 

Unfortunately, neither Google Chrome nor Firefox seems to be able to parse the '*' key. When I delete the complete "data" object in the console, I see that the (nested) keys for the data structure are "parse", "text" and '*' . I can even reset the text that I need to the key "*". I.e.

 var dat = data.parse.text; 

works. It’s just that this β€œ*” symbol does not want to be recognized.

Any ideas on how to fix this? Ideally, I would like to access a value that refers to the "*" key. I just don't know how to program it in javascript.

+6
source share
1 answer

The * character is not allowed as part of the identifier (variable name) in JavaScript, so the syntax does not work. Instead, you can use array / index notation to access properties using any string, regardless of whether it is a valid identifier:

 var dat = data.parse.text['*']; 
+8
source

Source: https://habr.com/ru/post/901449/


All Articles