JavaScript with JSON
JavaScript with JSON
[toc]
JSON to web
loaded this object into a JavaScript program,
parsed in a variable called superHeroes
access the data inside using superHeroes.homeTown superHeroes[‘active’] superHeroes[‘members’][1][‘powers’][2]
Converting between objects and text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const header = document.querySelector('header');
const section = document.querySelector('section');
let requestURL = 'https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json';
let request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'text'; // now we're getting a string!
request.send();
request.onload = function() {
const superHeroesText = request.response; // get the string from the response
const superHeroes = JSON.parse(superHeroesText); // convert it to an object
populateHeader(superHeroes);
showHeroes(superHeroes);
}
let myJSON = { "name": "Chris", "age": "38" };
myJSON
let myString = JSON.stringify(myJSON);
myString
This post is licensed under CC BY 4.0 by the author.
Comments powered by Disqus.