如何计算React Native/JS中JSON响应的长度?

Dee*_*eee 3 javascript

我想计算我的回复JSON的长度.这是我的代码:

getMoviesFromApiAsync() {
return fetch('http://sampleurl.com/' + CommonDataManager.getInstance().getUserID())
    .then((response) => response.json())
    .then((responseJson) => {
        this.setState({isLoading: false, listMessages: responseJson});    
    })
    .catch((error) => {
        console.error(error);
    });
}
Run Code Online (Sandbox Code Playgroud)

Joe*_*oyd 10

计算钥匙

  var myObject = {'key':'something', 'other-key':'something else','another-key': 'another thing'}
  var count = Object.keys(myObject).length;
Run Code Online (Sandbox Code Playgroud)

myObject你的json响应在哪里,count是你对象的长度

这是正确计算json对象的最佳方法

在你的代码中,你应该在你的第二个中添加它 .then

var count = Object.keys(responseJson).length;
Run Code Online (Sandbox Code Playgroud)