js如何通过不同的Json数据类型进行映射
Js how to map through different Json data types
我在 javascript 中使用 fetch api。但是,我无法通过不同的 JSON 对象格式进行映射。这是我的代码的两个片段,第一个有效,第二个无效,因为 JSON 格式。
效果很好 --
fetch('https://randomuser.me/api/?results=3')
.then(results => { return results.json(); })
.then(function(data) {
{data.results.map((key) => console.log(key.gender))}
});
这不起作用 -- 因为 JSON 格式 ID 不同
fetch('https://jsonplaceholder.typicode.com/users/1')
.then(results => { return results.json(); })
.then(function(data) {
{data.results.map((key) => console.log(key.gender))}
});
这是我得到的错误
未捕获(承诺)类型错误:无法读取未定义的 属性 'map'
在
谁能解释一下我如何映射不同的 json 格式类型
提前致谢
这个数据集中没有results
属性,只有一个"result":
https://jsonplaceholder.typicode.com/users/1
如果您使用此数据集,则没有理由映射单个项目:
https://jsonplaceholder.typicode.com/users/
你可以映射它,但是,数据集中没有 result
属性,项目上也没有 gender
属性...所以你会需要映射 data
对象和现有的 属性... 即 website
:
fetch('https://jsonplaceholder.typicode.com/users/')
.then(results => { return results.json(); })
.then(function(data) {
{data.map((key) => console.log(key.website))}
});
我在 javascript 中使用 fetch api。但是,我无法通过不同的 JSON 对象格式进行映射。这是我的代码的两个片段,第一个有效,第二个无效,因为 JSON 格式。
效果很好 --
fetch('https://randomuser.me/api/?results=3')
.then(results => { return results.json(); })
.then(function(data) {
{data.results.map((key) => console.log(key.gender))}
});
这不起作用 -- 因为 JSON 格式 ID 不同
fetch('https://jsonplaceholder.typicode.com/users/1')
.then(results => { return results.json(); })
.then(function(data) {
{data.results.map((key) => console.log(key.gender))}
});
这是我得到的错误
未捕获(承诺)类型错误:无法读取未定义的 属性 'map' 在
谁能解释一下我如何映射不同的 json 格式类型 提前致谢
这个数据集中没有results
属性,只有一个"result":
https://jsonplaceholder.typicode.com/users/1
如果您使用此数据集,则没有理由映射单个项目:
https://jsonplaceholder.typicode.com/users/
你可以映射它,但是,数据集中没有 result
属性,项目上也没有 gender
属性...所以你会需要映射 data
对象和现有的 属性... 即 website
:
fetch('https://jsonplaceholder.typicode.com/users/')
.then(results => { return results.json(); })
.then(function(data) {
{data.map((key) => console.log(key.website))}
});