【问题标题】:how do i iterate across a json array? [closed]我如何遍历一个 json 数组? [关闭]
【发布时间】:2013-07-03 22:28:04
【问题描述】:

如何从 javascript 中的以下数组中检索描述: 这是JS:

$(function () {
    var description = data.output.description;
    $("#myinput").suggest({
        "filter": "(all type:/automotive/model)",
        "lang": "en",
        "key": "***",
        "output": "(description)",
        'limit': 40,
    }).bind("fb-select", function (e, data) {
        $("#response2 pre").text(JSON.stringify(data, null, '  '));
        prettyPrint();
        $("#response2").show();
    }).bind("fb-select", function (e, data) {
        $('#output').append(data.name + ", " + data.output['description']);
    });
});

回复:

{
  "mid": "/m/03mnql",
  "id": "/en/ford_f-series",
  "name": "Ford F-Series",
  "notable": {
    "name": "Automobile Model",
    "id": "/automotive/model"
  },
  "lang": "en",
  "score": 88.680244,
  "output": {
    "description": {
      "wikipedia": [
        "The F-Series is a series of full-size pickup trucks from Ford Motor Company which has been sold continuously for over six decades. The most popular variant of the F-Series is the F-150. It was the best-selling vehicle in the United States for 24 years, currently the best-selling truck for 37 years, and the best selling vehicle in Canada, though this does not include combined sales of GM pickup trucks. In the tenth generation of the F-series, the F-250 and F-350 changed body style in 1998 and joined the Super Duty series.\nDuring the post-World War II era, smaller Canadian rural communities had access to either a Ford dealer or a Lincoln-Mercury-Meteor dealer, but not both; a Mercury-badged version was sold at Lincoln-Mercury-Meteor dealers there from 1946–68. Other than the grilles, trim, and badging, these trucks were identical to their Ford counterparts."
      ]
    }
  }
}

【问题讨论】:

  • 使用什么编程语言?
  • 如果您使用 JavaScript,那是对象文字,而不是 JSON。
  • @chepner 也许读过这个问题:“在 javascript 中”。不过,我必须承认“javascript”标签确实不见了。
  • 作为对上述观点的回应,我在我的 JS 中添加。感谢所有的回复非常非常快..

标签: javascript json


【解决方案1】:

如果这是一个对象字面量:

var description = data.output.description;

如果这还是 JSON:

var description = JSON.parse(yourstring).output.description;

但说真的,看起来你并没有付出很大的努力。

【讨论】:

  • 谢谢你的回答,你说得对,我想我没有付出很大的努力。
【解决方案2】:
objectVariableName.output.description;

这将返回一个具有单个键“wikipedia”的对象,该对象具有一个包含一个元素的数组值,这可能是您想要的描述。

objectVariableName.output.description.wikipedia[0]

【讨论】:

    猜你喜欢
    • 2019-04-14
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多