【发布时间】:2019-01-23 12:12:23
【问题描述】:
我试图获取我的 Json 响应的长度,但它不起作用 我在我的 json 结果中使用了“长度” 这是我在课堂上用 react js 尝试的:
componentDidMount() {
axios.get(`http://localhost:51492/api/CommentSurExperience/GetAllCommentOfExperience/${this.props.passedVal}`)
.then(res => {
const persons = res.data;
const longeur = res.length;
this.setState({ persons, longeur });
})
}
然后我返回了这个:
<div> <p> { this.state.longeur}</p> </div>
但我得到了一个空结果。
请问如何计算我的 JSON 响应中有多少元素 谢谢你的帮助。
**********更新*********** 这是我使用邮递员的 JSON 结果
这是一个显示 console.log(res) 显示内容的捕获
【问题讨论】:
-
您能展示一下
res的样貌吗? -
看起来
res是一个 json 对象而不是一个数组 -
@ChrisR 我更新了我的帖子,捕获了 Json 结果
-
你能把console.log(res)的结果写在这里吗?
-
那是邮递员 api 调用,你能 console.log(res) 吗?