假设我有一些变量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);


我正在尝试以定义列表的形式显示文本定义,例如:Internet搜索,例如在Google搜索引擎上执行的搜索。通过Google搜索引擎中的查询获得的匹配项。等

但是当我在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>";
});


给我第一个定义

最佳答案

使用.text将为您提供对象的此属性。

jQuery.each(a.definitions, function(i, val) {
    qr += '<li>' + val.text + '</li>';
});


如果要显示对象的索引,可以使用i如下所示:

qr += '<li>Index:' + i + ': ' + val.text + '</li>';

关于javascript - 作为每个jQuery语句读取多个JSON对象的文本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28511590/

10-09 19:26