async function load() { const data = await fetch('jsonUse.json'); const obj = await data.json(); console.log(obj); console.log(obj.one); console.log(obj.two); console.log(obj.two[0]); console.log(obj.two[0].twA); console.log(obj.two[0].twB); console.log(obj.two[1].tw3); console.log(obj.three.thA); console.log(obj.three.thB); console.log(obj.four); } load();
json
{ "one" : "基本", "two" : [ { "twA" : "配列", "twB" : "two[0].twB" }, { "tw3" : "two[1].tw3" } ], "three" : { "thA" : "three.thA", "thB" : "three.thB", "thC" : 3.3 }, "four" : "objectとjsonの違い -> キーはダブルクォートで囲む", "five" : "objectでは可能 function() { console.log(`${this.one}`); }", "six" : "[]内も{}内も最後の要素はコンマつけない" }
json,object実用例
・CSS const css = [ { pro : "display", val : ["block", "flex", "..."] }, { pro : "position", val : ["absolute", "fixed", "..."] } ];