0

Based on the question https://pt.stackoverflow.com/questions/323738/como-incluir-arquivo-json-dentro-de-um-js (in Portuguese), but he does not have an array and I have an array. I applied that and the variables were undefined due to a lack of an array that he did not.

You can test using to run the preview.

I also tried to apply the questions:

  1. get size of json object
  2. Multidimensional array from JSON object

    • Firstly in the HTML file:
  <p id="name">Name 1</p>
  <p id="age">Age 1</p>
  <p id="name">Name 2</p>
  <p id="age">Age 2</p>
  <p id="name">Name 3</p>
  <p id="age">Age 3</p>
  <p id="name">Name 4</p>
  <p id="age">Age 4</p>
  <p id="name">Name 5</p>
  <p id="age">Age 5</p>
  • JSON:
{
  "people": 
  [
    {
      "id": 0,
      "name": "Person Name 0",
      "age": 15
    },
    {
      "id": 1,
      "name": "Person Name 1",
      "age": 25
    },
    {
      "id": 2,
      "name": "Person Name 2",
      "age": 35
    },
    {
      "id": 3,
      "name": "Person Name 3",
      "age": 45
    },
    {
      "id": 4,
      "name": "Person Name 4",
      "age": 55
    },
    {
      "id": 5,
      "name": "Person Name 5",
      "age": 60
    }
  ]
}
  • Based on that question in Portuguese like:
function readTextFile(file, callback) {
  var rawFile = new XMLHttpRequest();
  rawFile.overrideMimeType("application/json");
  rawFile.open("GET", file, true);
  rawFile.onreadystatechange = function() {
      if (rawFile.readyState === 4 && rawFile.status == "200") {
          callback(rawFile.responseText);
      }
  }
  rawFile.send(null);
}

readTextFile("../json/people.json", function(text)
{
  var people = JSON.parse(text);

  var name = document.getElementById('name');
  name.innerHTML = people.name;

  var age = document.getElementById('age');
  age.innerHTML = people.age;

  console.log(people);


});
  • Or using Object.keys, like:
readTextFile("../json/people.json", function(text){
  var people = JSON.parse(text);

  var name = document.getElementById('name');
  name.innerHTML = Object.keys(people.name).length;

  var age = document.getElementById('age');
  age.innerHTML = Object.keys(people.age).length;

  console.log(people);


});

function readTextFile(file, callback) {
  var rawFile = new XMLHttpRequest();
  rawFile.overrideMimeType("application/json");
  rawFile.open("GET", file, true);
  rawFile.onreadystatechange = function() {
      if (rawFile.readyState === 4 && rawFile.status == "200") {
          callback(rawFile.responseText);
      }
  }
  rawFile.send(null);
}

readTextFile("https://pastebin.com/raw/CzxURs8y", function(text)
{
  var people = JSON.parse(text);

  var name = document.getElementById('name');
  name.innerHTML = people.name;

  var age = document.getElementById('age');
  age.innerHTML = people.age;

  console.log(people);


});
  <p id="name">Name 1</p>
  <p id="age">Age 1</p>
  <p id="name">Name 2</p>
  <p id="age">Age 2</p>
  <p id="name">Name 3</p>
  <p id="age">Age 3</p>
  <p id="name">Name 4</p>
  <p id="age">Age 4</p>
  <p id="name">Name 5</p>
  <p id="age">Age 5</p>

6
  • 2
    Element ID should always be unique. Commented Mar 1, 2020 at 22:55
  • Although browsers will do something with duplicate id values, results are unpredictable. This needs fixing as the first step. Commented Mar 1, 2020 at 22:56
  • @JayB and @traktor53, the problem is that my teacher requires to keep people, I can not change the JSON. Commented Mar 1, 2020 at 22:57
  • your json is an object where a key named people contains your array you need to access it accordingly, eg data =JSON.parse(text); data.people[0].name Commented Mar 1, 2020 at 23:02
  • @GustavoReis the html element id always needs to be unique (change the html, not the json) Commented Mar 1, 2020 at 23:05

1 Answer 1

1
fetch("../json/people.json")
    .then(r=>r.json())
    .then(json=>{
        const nameElms = Array.from(document.querySelectorAll("[id='name']"));
        const ageElms = Array.from(document.querySelectorAll("[id='age']"));
        nameElms.forEach((n,i)=>{
            const person = json.people[i];
            n.textContent = person.name;
        })
        ageElms.forEach((n,i)=>{
            const person = json.people[i];
            n.textContent = person.age;
        })
    })
Sign up to request clarification or add additional context in comments.

3 Comments

Are you sure that it supports inner.HTML?
it almost supports innerHTML, but it does not support. Too bad. In HTML, <div id="image"></div>, and in JavaScript: imageElms.forEach((n, i) => { const product = json.products[i]; let images =`<img src="https:${product.image}" />`; n.textContent.innerHTML =+ images; }).
n.innerHTML = images

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.