【问题标题】:how to retrive data from JSON in javascript @ [duplicate]如何在javascript @ [重复]中从JSON中检索数据
【发布时间】:2022-01-09 07:16:16
【问题描述】:
let data = {
  
  "@type": "Movie",
  "url": "/title/tt0443272/",
  "name": "Lincoln",
  "image": "https://m.media-amazon.com/images/M/MV5BMTQzNzczMDUyNV5BMl5BanBnXkFtZTcwNjM2ODEzOA@@._V1_.jpg",
  "description": "As the American Civil War continues to rage, America's president struggles with continuing carnage on the battlefield as he fights with many inside his own cabinet on the decision to emancipate the slaves.",
   
 
 
  
  "duration": "PT2H30M"
}


console.log(data.@type)

我有来自 scrping 的大量 json 数据,但我不知道如何获取其数据的价值 键从“@”开始

我如何获得@type 的值

当我使用这种方法时,我得到了错误:-

SyntaxError: 无效或意外的令牌

【问题讨论】:

  • 试试data["@type"]

标签: javascript json


【解决方案1】:

只要console.log(data['@type'])

let data = {
  "@type": "Movie",
  "url": "/title/tt0443272/",
  "name": "Lincoln",
  "image": "https://m.media-amazon.com/images/M/MV5BMTQzNzczMDUyNV5BMl5BanBnXkFtZTcwNjM2ODEzOA@@._V1_.jpg",
  "description": "As the American Civil War continues to rage, America's president struggles with continuing carnage on the battlefield as he fights with many inside his own cabinet on the decision to emancipate the slaves.",
  "duration": "PT2H30M"
}


console.log(data['@type'])

【讨论】:

  • 这是我 TNX 的工作:)
【解决方案2】:

您也可以使用Bracket notation 访问变量属性,例如它的键 -> 值:

let data = {

    "@type": "Movie",
    "url": "/title/tt0443272/",
    "name": "Lincoln",
    "image": "https://m.media-amazon.com/images/M/MV5BMTQzNzczMDUyNV5BMl5BanBnXkFtZTcwNjM2ODEzOA@@._V1_.jpg",
    "description": "As the American Civil War continues to rage, America's president struggles with continuing carnage on the battlefield as he fights with many inside his own cabinet on the decision to emancipate the slaves.",

    "duration": "PT2H30M"
}


console.log(data["@type"])

【讨论】:

    【解决方案3】:

    JavaScript 中的变量只能以字母、美元符号或下划线开头。

    当使用无效的变量名时,不能使用点表示法,必须使用方括号表示法

    无效

    data.@type
    

    正确

    data['@type']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 2019-05-01
      • 1970-01-01
      • 2016-03-07
      相关资源
      最近更新 更多