Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
Cómo obtener un valor JSON usando Javascript en una variable

Así que tengo este JSON:

 { "success": { "total": 1 }, "contents": { "quotes": [ { "quote": "When you win, say nothing. When you lose, say less.", "length": "51", "author": "Paul Brown", "tags": { "0": "inspire", "1": "losing", "2": "running", "4": "winning" }, "category": "inspire", "language": "en", "date": "2022-03-05", "permalink": "https://theysaidso.com/quote/paul-brown-when-you-win-say-nothing-when-you-lose-say-less", "id": "3dlKxoNAOZsB__Nb61H95weF", "background": "https://theysaidso.com/img/qod/qod-inspire.jpg", "title": "Inspiring Quote of the day" } ] }, "baseurl": "https://theysaidso.com", "copyright": { "year": 2024, "url": "https://theysaidso.com" } }

Estoy tratando de capturar el texto en los campos de autor y cita en dos variables respectivas. Ya asigné la variable JSON al contenido de json. Lo que tengo ahora es lo siguiente a continuación que no funciona.

 QUOTE = JSON.contents.quotes[0].quote; AUTHOR = JSON.contents.quotes[0].author;

¿Qué estoy haciendo mal? ¿Y cuál sería el enfoque correcto?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

¿Necesitaste el archivo json? Funciona bien para mí. también agregue let o var antes de la variable

 var json = require('./2.json') let QUOTE = json.contents.quotes[0].quote; let AUTHOR = json.contents.quotes[0].author; console.log(QUOTE, AUTHOR);

esta salida es When you win, say nothing. When you lose, say less. Paul Brown

about 4 years ago · Juan Pablo Isaza Report

0

Ya asigné la variable JSON al contenido de json.

Según su explicación, asumo que asignó el objeto de contents a la variable JSON . En ese caso, necesitaba acceder a las propiedades mediante JSON.quotes[0].quote y JSON.quotes[0].author . Consulte el fragmento a continuación.

 var yourObject = { "success": { "total": 1 }, "contents": { "quotes": [{ "quote": "When you win, say nothing. When you lose, say less.", "length": "51", "author": "Paul Brown", "tags": { "0": "inspire", "1": "losing", "2": "running", "4": "winning" }, "category": "inspire", "language": "en", "date": "2022-03-05", "permalink": "https://theysaidso.com/quote/paul-brown-when-you-win-say-nothing-when-you-lose-say-less", "id": "3dlKxoNAOZsB__Nb61H95weF", "background": "https://theysaidso.com/img/qod/qod-inspire.jpg", "title": "Inspiring Quote of the day" }] }, "baseurl": "https://theysaidso.com", "copyright": { "year": 2024, "url": "https://theysaidso.com" } } let JSON = yourObject.contents let yourQuote = JSON.quotes[0].quote let yourAuthor = JSON.quotes[0].author console.log(yourQuote) console.log(yourAuthor)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!