4.1 KiB
4.1 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7fae367417b2b2512be4 | Access the JSON Data from an API | 6 | 从API访问JSON数据 |
Description
[] - >方括号代表一个数组了解API返回的数据结构非常重要,因为它会影响您检索所需值的方式。在右侧,单击“获取消息”按钮,将freeCodeCamp Cat Photo API JSON加载到HTML中。您在JSON数据中看到的第一个和最后一个字符是方括号
{} - >圆括号代表一个对象
“” - >双引号代表一个字符串。它们还用于JSON中的键名
[ ]
。这意味着返回的数据是一个数组。 JSON数据中的第二个字符是一个卷曲{
括号,它启动一个对象。仔细观察,您可以看到有三个独立的对象。 JSON数据是一个包含三个对象的数组,其中每个对象都包含有关猫照片的信息。您之前了解到,对象包含以逗号分隔的“键值对”。在Cat Photo示例中,第一个对象具有"id":0
其中“id”是键,0是其对应值。类似地,有“imageLink”,“altText”和“codeNames”的键。每个猫照片对象具有相同的键,但具有不同的值。第一个对象中另一个有趣的“键值对”是"codeNames":["Juggernaut","Mrs. Wallace","ButterCup"]
。这里“codeNames”是键,它的值是三个字符串的数组。可以有对象数组以及带有数组作为值的键。记住如何访问数组和对象中的数据。数组使用括号表示法来访问项目的特定索引。对象使用括号或点表示法来访问给定属性的值。这是打印第一张猫照片的“altText”的示例 - 请注意编辑器中解析的JSON数据保存在名为json
的变量中: 的console.log(JSON [0] .altText);
//打印“头上戴着绿色头盔形瓜的白猫。”
Instructions
codeNames
数组中向控制台输出第二个值。您应该在对象上使用括号和点表示法(保存在变量json
)来访问该值。 Tests
tests:
- text: 您的代码应使用括号和点符号来访问正确的代码名称,并将“Loki”打印到控制台。
testString: 'assert(code.match(/(?:json\[2\]\.codeNames\[1\]|json\[2\]\[("|")codeNames\1\]\[1\])/g), "Your code should use bracket and dot notation to access the proper code name, and print "Loki" to the console.");'
Challenge Seed
<script>
document.addEventListener('DOMContentLoaded',function(){
document.getElementById('getMessage').onclick=function(){
req=new XMLHttpRequest();
req.open("GET",'/json/cats.json',true);
req.send();
req.onload=function(){
json=JSON.parse(req.responseText);
document.getElementsByClassName('message')[0].innerHTML=JSON.stringify(json);
// Add your code below this line
// Add your code above this line
};
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
Solution
// solution required