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)tengo grandes datos json de scrping pero no sé cómo obtener el valor de los datos cuya clave comienza con "@"
¿Cómo puedo obtener el valor de @type?
cuando uso este método, tengo un error: -
SyntaxError: token no válido o inesperado
Solo haz 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'])También puede acceder a las propiedades de las variables usando la notación de soporte , como si fuera una clave -> valor:
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"])Las variables en JavaScript solo pueden comenzar con una letra, un signo de dólar o un guión bajo.
Cuando usa un nombre de variable no válido, no puede usar la notación de puntos y debe usar la notación de corchetes
Inválido
data.@typeCorrecto
data['@type']