【问题标题】:JSON jQuery auto-fill [closed]JSON jQuery自动填充[关闭]
【发布时间】:2021-12-30 22:21:09
【问题描述】:

首先,我真的没有学过任何javascript,我被困在这里了。

我想在这个 JSON 示例中获取这些 link

"list": [
    [
      "List of recipes",
      [
        {
          "name": "Eggs",
          "links": [
            {
              "method": "Fry",
              "link": "https://example.com/category/fry"
            },
          ]
        },
      ]
    ]

我无法详细说明我朋友的示例代码,谁能帮助我?

【问题讨论】:

  • 你想要链接数组吗?注意:这不是有效的 JSON。
  • “我真的根本没有学过任何 javascript”,这就是你被卡住的原因。当您无法执行一项简单的任务时,从一项复杂的任务开始不会有好的结果。我想说,去参加 JS 基础/编程基础课程。例如可汗学院的这个:khanacademy.org/computing/computer-programming/programming

标签: javascript jquery json


【解决方案1】:
  1. 您的 JSON 无效,因此您需要先修复它。

  2. 当它有效时用JSON.parse解析它。

  3. 解析后,您将拥有一个嵌套数组数据结构。您需要通过使用数组索引在树中导航来访问链接数组。


// Returns an array
data.list[0]

// Get the second element of that array
// (the first element is the text "List of recipes")
// which is also an array
data.list[0][1]

// Access the first element of that array
// (an object) and return the links array
data.list[0][1][0].links

const json = '{"list":[["List of recipes",[{"name":"Eggs","links":[{"method":"Fry","link":"https://example.com/category/fry"}]}]]]}';

const data = JSON.parse(json);

console.log(data.list[0][1][0].links);

【讨论】:

    猜你喜欢
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多