简体   繁体   中英

How do you use special character ids from an API request?

Consider the following AJAX request:

$.ajax({
    url: 'http://www.minecraftwiki.net/api.php?action=query&meta=siteinfo&siprop=namespaces&format=json',
    dataType: 'json',
    async: false,
    success: function(siResponse) {
        for(var ns in siResponse.query.namespaces) {
            if(siResponse.query.namespaces[ns].id >= 0) {
                if(siResponse.query.namespaces[ns].id != namespaces.length) {
                    break;
                }
                if(siResponse.query.namespaces[ns].id === 0) {
                    namespaces[siResponse.query.namespaces[ns].id] = 'Main';
                } else {
                    namespaces[siResponse.query.namespaces[ns].id] = siResponse.query.namespaces[ns].&asterisk;;
                }
                movelog[siResponse.query.namespaces[ns].id] = 0;
                protectlog[siResponse.query.namespaces[ns].id] = 0;
            }
        }
    }
});

Here's the API response

Now, I could just use .canonical and rename the ids 4 and 5 from the default "Project" to "Minecraft Wiki", but I would like to use the "*": value instead. Is that possible, or is this just a bad wiki setting?

To the best of my knowledge: no, you cannot begin an id with an asterisk, number, or other special characters. See this post for more info.

You need to use foo['*'] to access the field instead of foo.* which would be a SyntaxError.

So in your case it would be siResponse.query.namespaces[ns]['*']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM