响应本机 json 响应



我正在尝试通过控制台打印 json 响应.log ,下面是我的 json 响应,但我希望每个元素像 user.id 或 product.name 一样一个接一个地打印

{
"user": [{
"id": "1",
"name": "User 1"
}, {
"id": "2",
"name": "User 2"
}],
"user_pictures": false,
"products": [{
"id": "1",
"name": "test abc"
}, {
"id": "2",
"name": "test abc 1"
}],
"purpose": ""
}

我正在尝试这样做:

responseData.map((item)=>{
console.log(item.user.id)
})

但控制台中显示错误

TypeError: responseData.map is not a function

responseData 是我调用我的 fetch 方法时 json 中的响应

您可以像这样input.user[0].id访问id

以下是工作代码:

let input = {
"user": [{
"id": "1",
"name": "User 1"
}, {
"id": "2",
"name": "User 2"
}],
"user_pictures": false,
"products": [{
"id": "1",
"name": "test abc"
}, {
"id": "2",
"name": "test abc 1"
}],
"purpose": ""
};
console.log(input.user[0].id);

responseData 是一个对象...您无法在对象上映射...响应数据有 4 个属性...用户、user_pictures、产品、目的...其中,responseData.usersresponseData.products是可以映射的数组。 user_pictures是一个布尔值,目的是一个字符串。

它的工作原理是这样的:

JSON.parse(responseData).user.map((item)=>{
console.log(item.id)
})

谢谢大家

最新更新