首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将多个JSON对象的文本作为每个jQuery语句读取?

将多个JSON对象的文本作为每个jQuery语句读取?
EN

Stack Overflow用户
提问于 2015-02-14 01:51:49
回答 2查看 536关注 0票数 0

假设我有一些变量a的JSON:

代码语言:javascript
复制
{"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"}
]}

这是每条语句

代码语言:javascript
复制
var qr="";
jQuery.each(a.definitions, function(i,val) {
    qr +="<li>"+ val + "</li>";
});
$('#someDIV p').replaceWith(qr);

我试图以如下定义列表的形式显示文本定义:

  • 一种互联网搜索,例如在Google搜索引擎上执行的搜索。
  • 由Google搜索引擎中的查询获得的匹配。
  • 等等

但是当我在jQuery.each(a.definition)中使用jQuery时,它给了我

代码语言:javascript
复制
<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>

如何读取对象的文本(即定义),以便给出定义,而不是对象对象,类似于如何

代码语言:javascript
复制
jQuery.each(a.definitions[0], function(i,val) {
    qr +="<li>"+ val + "</li>";
});

给了我第一个定义

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-14 01:56:30

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

代码语言:javascript
复制
jQuery.each(a.definitions, function(i, val) {
    qr += '<li>' + val.text + '</li>';
});

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

代码语言:javascript
复制
qr += '<li>Index:' + i + ': ' + val.text + '</li>';
票数 2
EN

Stack Overflow用户

发布于 2015-02-14 01:54:47

假设您希望显示每个条目的text属性:

代码语言:javascript
复制
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:

代码语言:javascript
复制
{
    "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都是如下所示的对象:

代码语言:javascript
复制
{
    "text": "To be locatable in a search of the Internet.",
    "attribution": "from Wiktionary, Creative Commons Attribution/Share-Alike License"
}

因此,您必须像我前面提供的代码那样单独访问这些属性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28511590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档