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

98
Visualizações
Can't seem to reference data in a complex JSON file, probably just my syntax

I am a noob so please forgive me. I have the following JSON file:

`{ "Series": {"Next":

[ { "Name": "Cheese", "Price" : 2.50, "Location": "Refrigerated foods"}, 
{ "Name": "Crisps", "Price" : 3, "Location": "the Snack isle"}, 
{ "Name": "Pizza", "Price" : 4, "Location": "Refrigerated foods"}, 
{ "Name": "Chocolate", "Price" : 1.50, "Location": "the Snack isle"}, 
{ "Name": "Self-raising flour", "Price" : 1.50, "Location": "Home baking"}, 
{ "Name": "Ground almonds", "Price" : 3, "Location": "Home baking"} ]}`

I'm trying to reference the Name, Location and Price but it's now working. Here is the JavaScript code I am using:

.then(function(json) {
      for(var i = 0; i < json.Next.length; i++) {
        var listItem = document.createElement('li_prod');
        listItem.innerHTML = '<strong_prod>' + json.Next[i].Name + '</strong_prod>';
        listItem.innerHTML +=' can be found in ' + json.Next[i].Location + '.';
        listItem.innerHTML +=' Cost: <strong_prod>£' + json.Next[i].Price + '</strong_prod>';
        myList.appendChild(listItem);
      }

that code is not picking up the Name, Location or price. How can I reference the data? Something like json.Series.Next[i].Name?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

with the structure of your object it's more json.Series.Next instead of json.Next

var json = {
  "Series": {
    "Next":

      [{
          "Name": "Cheese",
          "Price": 2.50,
          "Location": "Refrigerated foods"
        },
        {
          "Name": "Crisps",
          "Price": 3,
          "Location": "the Snack isle"
        },
        {
          "Name": "Pizza",
          "Price": 4,
          "Location": "Refrigerated foods"
        },
        {
          "Name": "Chocolate",
          "Price": 1.50,
          "Location": "the Snack isle"
        },
        {
          "Name": "Self-raising flour",
          "Price": 1.50,
          "Location": "Home baking"
        },
        {
          "Name": "Ground almonds",
          "Price": 3,
          "Location": "Home baking"
        }
      ]
  }
}

var myList = document.getElementById('my-list');

for (var i = 0; i < json.Series.Next.length; i++) {
  var listItem = document.createElement('li_prod');
  listItem.innerHTML = '<strong_prod>' + json.Series.Next[i].Name + '</strong_prod>';
  listItem.innerHTML += ' can be found in ' + json.Series.Next[i].Location + '.';
  listItem.innerHTML += ' Cost: <strong_prod>£' + json.Series.Next[i].Price + '</strong_prod>';
  myList.appendChild(listItem);
}
<div id="my-list">
</div>

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