假设我有一些变量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中使用jQuery.each(a.definition)它给了我
<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>
如何读取对象的文本(这是定义),以便它给我的定义不是[object Object],如何
jQuery.each(a.definitions[0], function(i,val) {
qr +="<li>"+ val + "</li>";
});
给我第一个定义
答案 0 :(得分:2)
使用.text
会为您提供对象的此属性。
jQuery.each(a.definitions, function(i, val) {
qr += '<li>' + val.text + '</li>';
});
如果要显示对象的索引,可以使用i
这样的内容:
qr += '<li>Index:' + i + ': ' + val.text + '</li>';
答案 1 :(得分:1)
您的定义由需要进一步解析的对象数组组成。使用val.text
查看说明文字。使用控制台查看循环的实际输出也很有帮助。将变量换行到console.log(variable)
,控制台会向您显示有关val
的所有信息。
jQuery.each(a.definitions[0], function(i,val) {
console.log(val);
qr +="<li>"+ val.text + "</li>";
});
答案 2 :(得分:0)
假设您要显示每个条目的text
属性:
jQuery.each(a.definitions, function(i, val) {
qr += '<li>' + val.text + '</li>';
});
您还可以使用val.attribution
显示归因。
jQuery.each
上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
数组时,您在val
函数回调中收到的每个each
都是看起来像这样的对象:
{
"text": "To be locatable in a search of the Internet.",
"attribution": "from Wiktionary, Creative Commons Attribution/Share-Alike License"
}
因此,您必须单独访问属性,如上面提供的代码。