Use Object.keys(data)[x]
to replace the nominative pathway byt the the coordinate of your data.
- Object.keys(data) -- give you the list of keys at that level.
- Then use
x
=the numeral rang of your target data. For the first data point, then [x]=[0].
Solution> JSfiddle:
function WD(val) { // input
target_API_url = "http://zh.wikipedia.org/w/api.php?action=query&prop=description&titles=" + val.toString() + "&prop=extracts&exintro&explaintext&format=json&redirects&callback=?";
$.getJSON(target_API_url, function (json) {
trad = json.query.redirects[0].to; // no "var", variable is global
var item_id = Object.keys(json.query.pages)[0]; // THIS DO THE TRICK !
sent = JSON.stringify(json.query.pages[item_id].extract);
result = "<b>En:</b> "+val.toString() + ", <b>Zh: </b>"+trad.toString() + "<br /><b>⇒</b>" + sent.toString();
$('p').html(result); // transformation
});
};
WD("WTO");
Encouraging +1 welcome.