读取多个JSON Object的文本作为每个jQuery语句?

时间:2015-02-14 01:51:50

标签: javascript jquery json

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

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

  • 互联网搜索,例如在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>";
    });
    

    给我第一个定义

    3 个答案:

    答案 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.eachapi.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"
    }
    

    因此,您必须单独访问属性,如上面提供的代码。