维基百科 API - 访问 JSON 对象
我正在尝试使用他们的 API(API 是他们提供的内容的一个慷慨术语,但我猜我们会使用它)从维基百科文章中提取文本,并且我遇到了有关解析后续 JSON 的问题我要回来的对象。 也就是说,该对象包含我在标签为“*”的键下查找的文本,这样,在运行命令后:
$.getJSON("http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {page:"Red Sea clownfish", prop:"text"}, function(data) {
然后我尝试使用以下命令将此信息解析为字符串:
var dat = data.parse.text.*;
然后将其输出到控制台using:
console.log(dat);
不幸的是,Google Chrome 和 Firefox 似乎都无法解析 '*'
键。当我将完整的“数据”对象转储到控制台时,我可以看到数据结构的(嵌套)键是“parse”、“text”和“*”。我什至可以将我需要的文本转储到“*”键。即
var dat = data.parse.text;
有效。只是'*'字符不想被识别。
关于如何解决这个问题有什么想法吗?理想情况下,我想访问“*”键引用的值。我只是不知道如何用 javascript 对其进行编程。
I'm trying to pull the text out of a Wikipedia article using their API (API is a generous term for what they are offering, but we'll use it I guess), and I am running into issues regarding parsing of the subsequent JSON object I am getting back.
Namely, the object contains the text I am looking for under a key whose label is '*' such that, after running the command:
$.getJSON("http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {page:"Red Sea clownfish", prop:"text"}, function(data) {
I then attempt to parse this information into a string using the command:
var dat = data.parse.text.*;
Which I am then outputting to a console using:
console.log(dat);
Unfortunately, neither Google Chrome nor Firefox seem to be able to parse the '*'
key. When I dump the full 'data' object into the console, I can see that the (nested) keys for the data structure are 'parse', 'text', and '*'
. I can even dump the text I need up to the '*' key. I.e.
var dat = data.parse.text;
works. It's just that '*' character does not want to be recognized.
Any ideas on how to fix this? Ideally I'd like to get access to the value that the '*' key is referencing. I just have no idea how to program it in javascript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JavaScript 中不允许使用
*
字符作为标识符(变量名称)的一部分,因此该语法不起作用。相反,您可以使用数组/下标表示法来访问使用任何字符串的属性,无论它是否是有效的标识符:The
*
character isn't allowed as part of an identifier (variable name) in JavaScript, so that syntax doesn't work. Instead, you can use the array/subscript notation to access properties using any string, regardless of whether it's a valid identifier: