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

159
Visualizações
How to get only an specific piece of json data

This is my first time with javascript, I know Its awful hahaha

I'm looking for a way to display only the currentDateTime value from the json, the number after the T to be more specific, when clicking the button, but everytime I click on the button, It displays all the json data. Is there a better way to do this, I mean, a correct way?

<!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">
    <title>World Clock</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1 class="msg">World Clock</h1>
    <p class="dsc">Click one of the buttons to see the current time</p>

    <button class="btn1" onclick="estFunc()">Eastern Standard Time (EST)</button>
fetch('http://worldclockapi.com/api/json/est/now')
    .then(response => response.json())
    .then(data => time = data)
    .then(() => console.log(time["currentDateTime"]))

function estFunc() {
    const obj = {time};
    const estJson = JSON.stringify(obj);
    document.getElementById("est").innerHTML = estJson;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

const estFunc = async () => {
  const response = await fetch('http://worldclockapi.com/api/json/est/now', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json'
    }
  });
  const data = await response.json(); //extract JSON from the http response
  // do something with JSON
  document.getElementById("est").innerHTML = data.currentDateTime;
}
<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">
  <title>World Clock</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <h1 class="msg">World Clock</h1>
  <p class="dsc">Click one of the buttons to see the current time</p>
  <button class="btn1" onclick="estFunc()">Eastern Standard Time (EST)</button>
  <div id="est"></div>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

The other answers are not wrong but don't provide an explanation on what's wrong. Your problem is that you stringify the whole object you get from the server, not just the currentDateTime.

fetch('http://worldclockapi.com/api/json/est/now')
    .then(response => response.json())
    .then(data => time = data.currentDateTime.time) // only assign currentDateTime.time instead of everything
    .then(() => console.log(time.currentDateTime.time))
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