I would like to count the length of my response JSON. Here's my code:
getMoviesFromApiAsync() {
return fetch('/' + CommonDataManager.getInstance().getUserID())
.then((response) => response.json())
.then((responseJson) => {
this.setState({isLoading: false, listMessages: responseJson});
})
.catch((error) => {
console.error(error);
});
}
I would like to count the length of my response JSON. Here's my code:
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);
});
}
Share
Improve this question
edited Sep 3, 2017 at 9:42
StudioTime
24k40 gold badges128 silver badges215 bronze badges
asked Sep 3, 2017 at 7:35
DeeeDeee
3292 gold badges9 silver badges27 bronze badges
2
- 2 Please read Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Commented Sep 3, 2017 at 8:19
- 2 You are welcome. The community also prefers titles written in natural English. Grammatically, "React How to count the length of a JSON response" doesn't make a lot of sense (though I understand the purpose of using "ReactJS" as a home-made tag). However, since we have a tagging system, it is better to merge the "tag" into the question, or get rid of it completely, since it is already in the tag list. – halfer Commented Sep 3, 2017 at 8:37
1 Answer
Reset to default 16Count the keys
var myObject = {'key':'something', 'other-key':'something else','another-key': 'another thing'}
var count = Object.keys(myObject).length;
Where myObject
is your json response and count
is the length of your object
This is the best way to count the json objects correctly
In your code you should add this in your second .then
var count = Object.keys(responseJson).length;