如何使用React JS计算JSON结果中的元素数量

Mar*_*met 1 api json count reactjs

我试图获取我的Json响应的长度,但是它不起作用,我在json结果中使用了“ length”,这就是我在我的班上在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 });
      })
  }
Run Code Online (Sandbox Code Playgroud)

然后我返回了这个:

<div> <p> { this.state.longeur}</p> </div>
Run Code Online (Sandbox Code Playgroud)

但是我的结果是空的。

我如何计算JSON响应中有多少个元素,请感谢您的帮助。

**********更新***********这是我使用邮递员的JSON结果 在此处输入图片说明

这是显示console.log(res)显示的内容的捕获

在此处输入图片说明

Rav*_*i R 6

它应该是

const longeur = res.data.length;
Run Code Online (Sandbox Code Playgroud)