That is jSON request
http://www.omdbapi.com/?s=harry
Follow the link to see the structure of response. How can I get a number of objects in order to create for loop
?
That is jSON request
http://www.omdbapi.com/?s=harry
Follow the link to see the structure of response. How can I get a number of objects in order to create for loop
?
First you need to JSON.parse the data (string) in this case:
var data = JSON.parse(string);
Getting the number of items is:
data.length;
You can do the same with a file (assuming node.js):
require('fs').readFile(JSON.parse(file), 'utf8', function (err, data
{
if (err) throw err;
// your data is now parsed
});
Then you iterate using a for-loop the array you're interested in:
for (var prop in data)
{
// do something with item
var item = data[prop];
}
Iteration nowadays doesn't require the a-priori knowledge of the array length:
for (var i = 0; i < data.length; ++i)
That JSON seems to have a Search
array of objects (see for your self: http://json.parser.online.fr/)
response.Search.map(function(obj){
//loop here
});
instead of using for loop