I'm trying to make a web application about movies with a movie API. Then I copied first two parts of search results and stored them in a variable; how can I get the specific data from variable?
I've considered like the JSON file is an object and tried to get the specific data but I couldn't.
This is the first two parts of search results that I stored in a variable.
var searchResults = {
page: 1,
total_results: 4109,
total_pages: 206,
results: [
{
original_name: 'Star',
id: 68780,
media_type: 'tv',
name: 'Star',
vote_count: 62,
vote_average: 7.12,
first_air_date: '2016-12-14',
popularity: 16.022,
original_language: 'en',
},
{
original_name: '부암동 복수자들',
id: 74473,
media_type: 'tv',
name: 'Avengers Social Club',
vote_count: 4,
vote_average: 9,
first_air_date: '2017-10-11',
popularity: 1.668,
original_language: 'ko',
}]};
I want to see names of this movies in console. How can I see names of this two movies in console.
I have tried this:
var options = {
method: 'GET',
url: 'api.themoviedb.org/3/search/multi',
qs: {
include_adult: 'false', page: '1',
query: 'star', language: 'en-US', api_key: 'I CANT SHOW THIS'
}, body: '{}'
};
request(options, function (error, response, body) {
if (error) {
console.log("ERROR!");
console.log(error);
} else {
if (response.statusCode == 200) {
var parsedBody = JSON.parse(body);
console.log(parsedBody["results"][0].name);
}
}
});
If you know how many results are returned in each JSON response, you can loop this way using a results count. For the two results in your example, it loops as long as i < 2
:
var searchResults = { page: 1, total_results: 4109, total_pages: 206, results: [ { original_name: 'Star', id: 68780, media_type: 'tv', name: 'Star', vote_count: 62, vote_average: 7.12, first_air_date: '2016-12-14', popularity: 16.022, original_language: 'en', }, { original_name: '부암동 복수자들', id: 74473, media_type: 'tv', name: 'Avengers Social Club', vote_count: 4, vote_average: 9, first_air_date: '2017-10-11', popularity: 1.668, original_language: 'ko', }]}; for (var i=0; i < 2; i++) { console.log(searchResults['results'][i].name); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.