Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
How could I show JSON from url in HTML using Javascript

good afternoon. I'm working on a project where I get a JSON and I have to display it in HTML and I'm not able to do that. In the console I can display all the information correctly, but I cannot display it in the HTML. What do I have to do? Here's the code and thanks for all the help. The error in console is:

Uncaught SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Object.success (index.html:21:26)
    at i (jquery-3.2.1.min.js:2:28017)
    at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2:28783)
    at A (jquery-3.2.1.min.js:4:14035)
    at XMLHttpRequest.<anonymous> (jquery-3.2.1.min.js:4:16323)

And HTML code:

    <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link id="mystylesheet" type="text/css" rel="stylesheet" href="style.css">

  <title>Document</title>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>

<body>
  <div id="wrapper">
    <h1 class="city"></h1>
    <h2><span id="tags">0</span>ºC</h2>
  </div>
  <script>
    const urlJsonString = $.getJSON('https://api.openweathermap.org/data/2.5/onecall?lat=38.7267&lon=-9.1403&exclude=current,hourly,minutely,alerts&units=metric&appid=ecef7e88947b6303121bb900373e41d2', function (data) {
      let urlJson = JSON.parse(urlJsonString)
      let [dt1, dt2, dt3, dt4, dt5, dt6, dt7, dt8] = urlJson.daily.map(({ dt, temp: { day }, weather: [{ description, icon }] }) => ({ dt, day, description, icon }))
      let result = `Data: ${urlJson}`
      $(".city").html(result);
    });
  </script>
</body>

</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The returned JSON object is already in the data parameter, so there's no need to parse anything. You could do let urlJson = data, or use data instead of urlJson altogether.

To show the JSON, use stringify, e.g.

var result = `Data: ${JSON.stringify(urlJson)}`;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda