假设我有一些变量a的JSON:
{"definitions":[
{"text":"An internet search, such as that which is performed on the Google search engine.","attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"},
{"text":"A match obtained by a query in the Google search engine.","attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"},
{"text":"To search for (something) on the Internet using the Google search engine.","attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"},
{"text":"To search for (something) on the Internet using any comprehensive search engine.","attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"},
{"text":"To be locatable in a search of the Internet.","attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"},
{"text":"To deliver googlies.","attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"},
{"text":"To move as a ball in a googly.","attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"}
]}这是每条语句
var qr="";
jQuery.each(a.definitions, function(i,val) {
qr +="<li>"+ val + "</li>";
});
$('#someDIV p').replaceWith(qr);我试图以如下定义列表的形式显示文本定义:
但是当我在jQuery.each(a.definition)中使用jQuery时,它给了我
<li>[object Object]</li>
<li>[object Object]</li>
<li>[object Object]</li>
<li>[object Object]</li>
<li>[object Object]</li>
<li>[object Object]</li>
<li>[object Object]</li>如何读取对象的文本(即定义),以便给出定义,而不是对象对象,类似于如何
jQuery.each(a.definitions[0], function(i,val) {
qr +="<li>"+ val + "</li>";
});给了我第一个定义
发布于 2015-02-14 01:56:30
使用.text将为您提供对象的这个属性。
jQuery.each(a.definitions, function(i, val) {
qr += '<li>' + val.text + '</li>';
});如果要显示对象的索引,可以使用i,如下所示:
qr += '<li>Index:' + i + ': ' + val.text + '</li>';发布于 2015-02-14 01:54:47
假设您希望显示每个条目的text属性:
jQuery.each(a.definitions, function(i, val) {
qr += '<li>' + val.text + '</li>';
});还可以使用val.attribution显示属性。
jQuery.each on api.jquery.com的文档。
对错误的解释
您获得[object Object]是因为您实际上是在对对象进行toString(),而不是对象的text属性中包含的文本。
为了便于解释,我在下面格式化了JSON:
{
"definitions": [{
"text": "An internet search, such as that which is performed on the Google search engine.",
"attribution": "from Wiktionary, Creative Commons Attribution/Share-Alike License"
}, {
"text": "A match obtained by a query in the Google search engine.",
"attribution": "from Wiktionary, Creative Commons Attribution/Share-Alike License"
}, {
"text": "To search for (something) on the Internet using the Google search engine.",
"attribution": "from Wiktionary, Creative Commons Attribution/Share-Alike License"
}, {
"text": "To search for (something) on the Internet using any comprehensive search engine.",
"attribution": "from Wiktionary, Creative Commons Attribution/Share-Alike License"
}, {
"text": "To be locatable in a search of the Internet.",
"attribution": "from Wiktionary, Creative Commons Attribution/Share-Alike License"
}, {
"text": "To deliver googlies.",
"attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"
}, {
"text": "To move as a ball in a googly.",
"attribution": "from Wiktionary, Creative Commons Attribution/Share-Alike License"
}]
}我想指出,在迭代a.definitions数组时,在each函数的回调中收到的每个val都是如下所示的对象:
{
"text": "To be locatable in a search of the Internet.",
"attribution": "from Wiktionary, Creative Commons Attribution/Share-Alike License"
}因此,您必须像我前面提供的代码那样单独访问这些属性。
https://stackoverflow.com/questions/28511590
复制相似问题