I have lot of objects like below in array now i want to apply filter on it. How i can apply filter like i have mention in image:
Can i use simple AngularJS filter on it?
Kindly help me with best approach to apply these filter. Thanks in advance. I have multiple hotels data like that in this JSON. But I am not butting it all because of space.
[
{
"_id": "57da6aeb4eafac04afd01bc5",
"name": "subways",
"city": "lahore",
"area": "johar town",
"min_order": "200 AED",
"del_time": "1 hour",
"del_fees": "1 AED",
"pre_order": "NA",
"rating_star": "4",
"rating_no": 514,
"status": "OPEN",
"__v": 0,
"categories": [
{
"_id": "57da72e94eafac04afd01bc9",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"name": "subways Burger",
"category_id": "1",
"__v": 0,
"items": [
{
"_id": "57da7973d0b5e2059c5ff10a",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 1",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
},
{
"_id": "57da79e8d0b5e2059c5ff10b",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 2",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
},
{
"_id": "57da79efd0b5e2059c5ff10c",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 3",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
},
{
"_id": "57da79fcd0b5e2059c5ff10d",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 4",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}
]
}
]
A simple AngularJS filter to handle such operations is shown below:
angular.module("myapp", [])
.controller("myctrl", function myctrl($scope) {
$scope.cityIncludes = [];
$scope.includeCity = function(city) {
console.log( city );
var i = $scope.cityIncludes.indexOf(city);
if (i > -1) {
$scope.cityIncludes.splice(i, 1);
} else {
$scope.cityIncludes.push(city);
}
console.log( $scope.cityIncludes);
}
$scope.cityFilter = function(city) {
if ($scope.cityIncludes.length > 0) {
if ($scope.cityIncludes.indexOf(city.city) < 0)
return;
}
return city;
}
$scope.rests = [{
"_id": "57da6aeb4eafac04afd01bc5",
"name": "Lahore Restaurant",
"city": "lahore",
"area": "johar town",
"min_order": "200 AED",
"del_time": "1 hour",
"del_fees": "1 AED",
"pre_order": "NA",
"rating_star": "4",
"rating_no": 514,
"status": "OPEN",
"__v": 0,
"categories": [{
"_id": "57da72e94eafac04afd01bc9",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"name": "subways Burger",
"category_id": "1",
"__v": 0,
"items": [{
"_id": "57da7973d0b5e2059c5ff10a",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 1",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79e8d0b5e2059c5ff10b",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 2",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79efd0b5e2059c5ff10c",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 3",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79fcd0b5e2059c5ff10d",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 4",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}]
}]
}, {
"_id": "57da6aeb4eafac04afd01bc5",
"name": "islamabad rest",
"city": "islamabad",
"area": "johar town",
"min_order": "200 AED",
"del_time": "1 hour",
"del_fees": "1 AED",
"pre_order": "NA",
"rating_star": "4",
"rating_no": 514,
"status": "OPEN",
"__v": 0,
"categories": [{
"_id": "57da72e94eafac04afd01bc9",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"name": "subways Burger",
"category_id": "1",
"__v": 0,
"items": [{
"_id": "57da7973d0b5e2059c5ff10a",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 1",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79e8d0b5e2059c5ff10b",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 2",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79efd0b5e2059c5ff10c",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 3",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79fcd0b5e2059c5ff10d",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 4",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}]
}]
}, {
"_id": "57da6aeb4eafac04afd01bc5",
"name": "abcd",
"city": "rawalpindi",
"area": "johar town",
"min_order": "200 AED",
"del_time": "1 hour",
"del_fees": "1 AED",
"pre_order": "NA",
"rating_star": "4",
"rating_no": 514,
"status": "OPEN",
"__v": 0,
"categories": [{
"_id": "57da72e94eafac04afd01bc9",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"name": "subways Burger",
"category_id": "1",
"__v": 0,
"items": [{
"_id": "57da7973d0b5e2059c5ff10a",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 1",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79e8d0b5e2059c5ff10b",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 2",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79efd0b5e2059c5ff10c",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 3",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79fcd0b5e2059c5ff10d",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 4",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}]
}]
}, {
"_id": "57da6aeb4eafac04afd01bc5",
"name": "asdadsasdasdas",
"city": "karachi",
"area": "johar town",
"min_order": "200 AED",
"del_time": "1 hour",
"del_fees": "1 AED",
"pre_order": "NA",
"rating_star": "4",
"rating_no": 514,
"status": "OPEN",
"__v": 0,
"categories": [{
"_id": "57da72e94eafac04afd01bc9",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"name": "subways Burger",
"category_id": "1",
"__v": 0,
"items": [{
"_id": "57da7973d0b5e2059c5ff10a",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 1",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79e8d0b5e2059c5ff10b",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 2",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79efd0b5e2059c5ff10c",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 3",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79fcd0b5e2059c5ff10d",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 4",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}]
}]
}, {
"_id": "57da6aeb4eafac04afd01bc5",
"name": "multan asdasda",
"city": "multan",
"area": "johar town",
"min_order": "200 AED",
"del_time": "1 hour",
"del_fees": "1 AED",
"pre_order": "NA",
"rating_star": "4",
"rating_no": 514,
"status": "OPEN",
"__v": 0,
"categories": [{
"_id": "57da72e94eafac04afd01bc9",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"name": "subways Burger",
"category_id": "1",
"__v": 0,
"items": [{
"_id": "57da7973d0b5e2059c5ff10a",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 1",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79e8d0b5e2059c5ff10b",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 2",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79efd0b5e2059c5ff10c",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 3",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}, {
"_id": "57da79fcd0b5e2059c5ff10d",
"restaurant_id": "57da6aeb4eafac04afd01bc5",
"category_id": "57da72e94eafac04afd01bc9",
"logo": "abcLogo",
"name": "subway item 4",
"detail": "great item",
"price": "10",
"rating": "5",
"__v": 0
}]
}]
}]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myapp" ng-controller="myctrl">
<ul>
<li ng-repeat="rest in rests track by rest.city">
<input name="rest.city" type="checkbox" value="rest.city" ng-click="includeCity(rest.city)">
<label for="rest.city">{{rest.city}}</label>
</li>
</ul>
<ul>
<li ng-repeat="rest in rests | filter: cityFilter">
<p ng-bind="rest.name"></p>
<p ng-bind="'Rating ' + rest.rating_star"></p>
<p ng-bind="'Status ' + rest.status"></p>
<p ng-bind="'Area ' + rest.area"></p>
</li>
</ul>
</body>
You can use this concept.
i have done
i have radio box for all type of cuisines when any one select. i save it into scope type variable with below function. now question is how i filter it bro.
<ul>
<li>
<input name="rest.cusine" type="radio" value="india" ng-click="filterCuisine('indian')">
<label for="open status">india</label>
</li>
<li>
<input name="rest.cusine" type="radio" value="india" ng-click="filterCuisine('pakistani')">
<label for="open status">pakistan</label>
</li>
<li>
<input name="rest.cusine" type="radio" value="arabic" ng-click="filterCuisine('arabic')">
<label for="open status">arabic</label>
</li>
</ul>
Function for getting the selected cuisine
$scope.filterCuisine = function(cuisine){
$scope.checkFilterCuisine = cuisine;
console.log($scope.checkFilterCuisine);
}
how i apply filter on this function
$scope.selectCuisine = function(city){
console.log(city.cuisines);
if (city.cuisines.length > 0 && $scope.checkFilterCuisine != '') {
if (city.cuisines.indexOf($scope.checkFilterCuisine) < 0){
return;
}
}
return city;
}
I am trying to parse JSON data like this:
var baseUrl = 'https://api.themoviedb.org/3/movie/'
var movieID = '550'
var detailUrl = '&append_to_response=releases,trailers,credits&callback=?'
var apiKey = '?api_key=Removed_For_Privacy'
The above url with the api key include returns this result:
?({
"adult": false,
"backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
"belongs_to_collection": null,
"budget": 63000000,
"genres": [
{
"id": 28,
"name": "Action"
},
{
"id": 18,
"name": "Drama"
},
{
"id": 53,
"name": "Thriller"
}
],
"homepage": "",
"id": 550,
"imdb_id": "tt0137523",
"original_title": "Fight Club",
"overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
"popularity": "10.2188172784825",
"poster_path": "/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg",
"production_companies": [
{
"name": "20th Century Fox",
"id": 25
},
{
"name": "Fox 2000 Pictures",
"id": 711
},
{
"name": "Regency Enterprises",
"id": 508
}
],
"production_countries": [
{
"iso_3166_1": "DE",
"name": "Germany"
},
{
"iso_3166_1": "US",
"name": "United States of America"
}
],
"release_date": "1999-10-14",
"revenue": 100853753,
"runtime": 139,
"spoken_languages": [
{
"iso_639_1": "en",
"name": "English"
}
],
"status": "Released",
"tagline": "How much can you know about yourself if you've never been in a fight?",
"title": "Fight Club",
"vote_average": 7.6,
"vote_count": 2787,
"releases": {
"countries": [
{
"iso_3166_1": "US",
"certification": "R",
"release_date": "1999-10-14"
},
{
"iso_3166_1": "DE",
"certification": "18",
"release_date": "1999-11-10"
},
{
"iso_3166_1": "GB",
"certification": "18",
"release_date": "1999-11-12"
},
{
"iso_3166_1": "FR",
"certification": "16",
"release_date": "1999-11-10"
},
{
"iso_3166_1": "TR",
"certification": "",
"release_date": "1999-12-10"
},
{
"iso_3166_1": "BR",
"certification": "feibris",
"release_date": "1999-07-12"
},
{
"iso_3166_1": "FI",
"certification": "K-18",
"release_date": "1999-11-12"
},
{
"iso_3166_1": "BG",
"certification": "c",
"release_date": "2012-08-28"
},
{
"iso_3166_1": "IT",
"certification": "VM14",
"release_date": "1999-10-29"
}
]
},
"trailers": {
"quicktime": [],
"youtube": [
{
"name": "Trailer 1",
"size": "HD",
"source": "SUXWAEX2jlg",
"type": "Trailer"
}
]
},
"credits": {
"cast": [
{
"id": 819,
"name": "Edward Norton",
"character": "The Narrator",
"order": 0,
"cast_id": 4,
"profile_path": "/iUiePUAQKN4GY6jorH9m23cbVli.jpg"
},
{
"id": 287,
"name": "Brad Pitt",
"character": "Tyler Durden",
"order": 1,
"cast_id": 5,
"profile_path": "/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"
},
{
"id": 1283,
"name": "Helena Bonham Carter",
"character": "Marla Singer",
"order": 2,
"cast_id": 6,
"profile_path": "/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"
},
{
"id": 7470,
"name": "Meat Loaf",
"character": "Robert 'Bob' Paulson",
"order": 3,
"cast_id": 7,
"profile_path": "/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"
},
{
"id": 7499,
"name": "Jared Leto",
"character": "Angel Face",
"order": 4,
"cast_id": 30,
"profile_path": "/msugySeTCyCmlRWtyB6sMixTQYY.jpg"
},
{
"id": 7471,
"name": "Zach Grenier",
"character": "Richard Chesler",
"order": 5,
"cast_id": 31,
"profile_path": "/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"
},
{
"id": 7497,
"name": "Holt McCallany",
"character": "The Mechanic",
"order": 6,
"cast_id": 32,
"profile_path": "/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"
},
{
"id": 7498,
"name": "Eion Bailey",
"character": "Ricky",
"order": 7,
"cast_id": 33,
"profile_path": "/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"
}
],
"crew": [
{
"id": 7469,
"name": "Jim Uhls",
"department": "Writing",
"job": "Author",
"profile_path": null
},
{
"id": 7474,
"name": "Ross Grayson Bell",
"department": "Production",
"job": "Producer",
"profile_path": null
},
{
"id": 7475,
"name": "Ceán Chaffin",
"department": "Production",
"job": "Producer",
"profile_path": null
},
{
"id": 1254,
"name": "Art Linson",
"department": "Production",
"job": "Producer",
"profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
},
{
"id": 7477,
"name": "John King",
"department": "Sound",
"job": "Original Music Composer",
"profile_path": null
},
{
"id": 7478,
"name": "Michael Simpson",
"department": "Sound",
"job": "Original Music Composer",
"profile_path": null
},
{
"id": 7479,
"name": "Jeff Cronenweth",
"department": "Camera",
"job": "Director of Photography",
"profile_path": null
},
{
"id": 7480,
"name": "James Haygood",
"department": "Editing",
"job": "Editor",
"profile_path": null
},
{
"id": 7481,
"name": "Laray Mayfield",
"department": "Production",
"job": "Casting",
"profile_path": null
},
{
"id": 1303,
"name": "Alex McDowell",
"department": "Art",
"job": "Production Design",
"profile_path": null
},
{
"id": 7763,
"name": "Ren Klyce",
"department": "Sound",
"job": "Sound Editor",
"profile_path": null
},
{
"id": 7764,
"name": "Richard Hymns",
"department": "Sound",
"job": "Sound Editor",
"profile_path": null
},
{
"id": 7467,
"name": "David Fincher",
"department": "Directing",
"job": "Director",
"profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
},
{
"id": 7468,
"name": "Chuck Palahniuk",
"department": "Writing",
"job": "Novel",
"profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
}
]
}
})
I use this to parse it, however i have no luck
$(document).ready(function() {
$.ajax({
url: baseUrl + movieID +apiKey +detailUrl,
dataType: "jsonp",
success: getGenres,
});
});
function getGenres(data) {
var entries = data
genre = 0,
genre_list = '';
for (genre = 0; genre < entries.genres.name.length; genre++) {
genre_list.push(entries.genres.name[genre]);
}
document.getElementById('Genres').innerHTML = genre_list.join(', ');
Please Help
entries.genres is an Array. It has no .name property. You should be getting an error in your browser's developer console for accessing .length of undefined.
{
"adult": false,
"backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
"belongs_to_collection": null,
"budget": 63000000,
"genres": [
{
"id": 28,
"name": "Action"
},
{
"id": 18,
"name": "Drama"
},
{
"id": 53,
"name": "Thriller"
}
],
...
}
So you need to iterate entries.genres, then push the .name of the current genre if that's what you want.
for (genre = 0; genre < entries.genres.length; genre++) {
genre_list.push(entries.genres[genre].name);
}
On a different note, you have two implicit globals.
var entries = data
genre = 0,
genre_list = '';
By forgetting the comma after var entries = data, the next two lines will implicitly create global variables since they're not part of the var statement.
That's why I always use leading commas for variable declarations. Makes it obvious when a comma is missing.
var entries = data
, genre = 0
, genre_list = '';
test.php
<?php
echo 'getGenres({
"adult": false,
"backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
"belongs_to_collection": null,
"budget": 63000000,
"genres": [
{
"id": 28,
"name": "Action"
},
{
"id": 18,
"name": "Drama"
},
{
"id": 53,
"name": "Thriller"
}
],
"homepage": "",
"id": 550,
"imdb_id": "tt0137523",
"original_title": "Fight Club",
"overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
"popularity": "10.2188172784825",
"poster_path": "/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg",
"production_companies": [
{
"name": "20th Century Fox",
"id": 25
},
{
"name": "Fox 2000 Pictures",
"id": 711
},
{
"name": "Regency Enterprises",
"id": 508
}
],
"production_countries": [
{
"iso_3166_1": "DE",
"name": "Germany"
},
{
"iso_3166_1": "US",
"name": "United States of America"
}
],
"release_date": "1999-10-14",
"revenue": 100853753,
"runtime": 139,
"spoken_languages": [
{
"iso_639_1": "en",
"name": "English"
}
],
"status": "Released",
"tagline": "How much can you know about yourself if youve never been in a fight?",
"title": "Fight Club",
"vote_average": 7.6,
"vote_count": 2787,
"releases": {
"countries": [
{
"iso_3166_1": "US",
"certification": "R",
"release_date": "1999-10-14"
},
{
"iso_3166_1": "DE",
"certification": "18",
"release_date": "1999-11-10"
},
{
"iso_3166_1": "GB",
"certification": "18",
"release_date": "1999-11-12"
},
{
"iso_3166_1": "FR",
"certification": "16",
"release_date": "1999-11-10"
},
{
"iso_3166_1": "TR",
"certification": "",
"release_date": "1999-12-10"
},
{
"iso_3166_1": "BR",
"certification": "feibris",
"release_date": "1999-07-12"
},
{
"iso_3166_1": "FI",
"certification": "K-18",
"release_date": "1999-11-12"
},
{
"iso_3166_1": "BG",
"certification": "c",
"release_date": "2012-08-28"
},
{
"iso_3166_1": "IT",
"certification": "VM14",
"release_date": "1999-10-29"
}
]
},
"trailers": {
"quicktime": [],
"youtube": [
{
"name": "Trailer 1",
"size": "HD",
"source": "SUXWAEX2jlg",
"type": "Trailer"
}
]
},
"credits": {
"cast": [
{
"id": 819,
"name": "Edward Norton",
"character": "The Narrator",
"order": 0,
"cast_id": 4,
"profile_path": "/iUiePUAQKN4GY6jorH9m23cbVli.jpg"
},
{
"id": 287,
"name": "Brad Pitt",
"character": "Tyler Durden",
"order": 1,
"cast_id": 5,
"profile_path": "/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"
},
{
"id": 1283,
"name": "Helena Bonham Carter",
"character": "Marla Singer",
"order": 2,
"cast_id": 6,
"profile_path": "/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"
},
{
"id": 7470,
"name": "Meat Loaf",
"character": "Robert Bob Paulson",
"order": 3,
"cast_id": 7,
"profile_path": "/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"
},
{
"id": 7499,
"name": "Jared Leto",
"character": "Angel Face",
"order": 4,
"cast_id": 30,
"profile_path": "/msugySeTCyCmlRWtyB6sMixTQYY.jpg"
},
{
"id": 7471,
"name": "Zach Grenier",
"character": "Richard Chesler",
"order": 5,
"cast_id": 31,
"profile_path": "/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"
},
{
"id": 7497,
"name": "Holt McCallany",
"character": "The Mechanic",
"order": 6,
"cast_id": 32,
"profile_path": "/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"
},
{
"id": 7498,
"name": "Eion Bailey",
"character": "Ricky",
"order": 7,
"cast_id": 33,
"profile_path": "/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"
}
],
"crew": [
{
"id": 7469,
"name": "Jim Uhls",
"department": "Writing",
"job": "Author",
"profile_path": null
},
{
"id": 7474,
"name": "Ross Grayson Bell",
"department": "Production",
"job": "Producer",
"profile_path": null
},
{
"id": 7475,
"name": "Ceán Chaffin",
"department": "Production",
"job": "Producer",
"profile_path": null
},
{
"id": 1254,
"name": "Art Linson",
"department": "Production",
"job": "Producer",
"profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
},
{
"id": 7477,
"name": "John King",
"department": "Sound",
"job": "Original Music Composer",
"profile_path": null
},
{
"id": 7478,
"name": "Michael Simpson",
"department": "Sound",
"job": "Original Music Composer",
"profile_path": null
},
{
"id": 7479,
"name": "Jeff Cronenweth",
"department": "Camera",
"job": "Director of Photography",
"profile_path": null
},
{
"id": 7480,
"name": "James Haygood",
"department": "Editing",
"job": "Editor",
"profile_path": null
},
{
"id": 7481,
"name": "Laray Mayfield",
"department": "Production",
"job": "Casting",
"profile_path": null
},
{
"id": 1303,
"name": "Alex McDowell",
"department": "Art",
"job": "Production Design",
"profile_path": null
},
{
"id": 7763,
"name": "Ren Klyce",
"department": "Sound",
"job": "Sound Editor",
"profile_path": null
},
{
"id": 7764,
"name": "Richard Hymns",
"department": "Sound",
"job": "Sound Editor",
"profile_path": null
},
{
"id": 7467,
"name": "David Fincher",
"department": "Directing",
"job": "Director",
"profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
},
{
"id": 7468,
"name": "Chuck Palahniuk",
"department": "Writing",
"job": "Novel",
"profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
}
]
}
})';
exit;
?>
javascript code:
<script language="javascript">
jq(document).ready(function() {
jq.ajax({
url: 'test.php',
dataType: "jsonp",
}); });
function getGenres(data){
alert(data.genres.length);
}
</script>
Your json response contains single quotes (') example 'bob' and you've which are not standard, so replace then by \' and then parse your json response.
For reference check jQuery single quote in JSON response
Remove ?( and ) from the starting and end of json response and also remove ' from them and check
var obj = jQuery.parseJSON( '{"adult":false,"backdrop_path":"/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg","belongs_to_collection":null,"budget":63000000,"genres":[{"id":28,"name":"Action"},{"id":18,"name":"Drama"},{"id":53,"name":"Thriller"}],"homepage":"","id":550,"imdb_id":"tt0137523","original_title":"Fight Club","overview":"A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground fight clubs forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.","popularity":"10.2188172784825","poster_path":"/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg","production_companies":[{"name":"20th Century Fox","id":25},{"name":"Fox 2000 Pictures","id":711},{"name":"Regency Enterprises","id":508}],"production_countries":[{"iso_3166_1":"DE","name":"Germany"},{"iso_3166_1":"US","name":"United States of America"}],"release_date":"1999-10-14","revenue":100853753,"runtime":139,"spoken_languages":[{"iso_639_1":"en","name":"English"}],"status":"Released","tagline":"How much can you know about yourself if youve never been in a fight?","title":"Fight Club","vote_average":7.6,"vote_count":2787,"releases":{"countries":[{"iso_3166_1":"US","certification":"R","release_date":"1999-10-14"},{"iso_3166_1":"DE","certification":"18","release_date":"1999-11-10"},{"iso_3166_1":"GB","certification":"18","release_date":"1999-11-12"},{"iso_3166_1":"FR","certification":"16","release_date":"1999-11-10"},{"iso_3166_1":"TR","certification":"","release_date":"1999-12-10"},{"iso_3166_1":"BR","certification":"feibris","release_date":"1999-07-12"},{"iso_3166_1":"FI","certification":"K-18","release_date":"1999-11-12"},{"iso_3166_1":"BG","certification":"c","release_date":"2012-08-28"},{"iso_3166_1":"IT","certification":"VM14","release_date":"1999-10-29"}]},"trailers":{"quicktime":[],"youtube":[{"name":"Trailer 1","size":"HD","source":"SUXWAEX2jlg","type":"Trailer"}]},"credits":{"cast":[{"id":819,"name":"Edward Norton","character":"The Narrator","order":0,"cast_id":4,"profile_path":"/iUiePUAQKN4GY6jorH9m23cbVli.jpg"},{"id":287,"name":"Brad Pitt","character":"Tyler Durden","order":1,"cast_id":5,"profile_path":"/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"},{"id":1283,"name":"Helena Bonham Carter","character":"Marla Singer","order":2,"cast_id":6,"profile_path":"/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"},{"id":7470,"name":"Meat Loaf","character":"Robert Bob Paulson","order":3,"cast_id":7,"profile_path":"/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"},{"id":7499,"name":"Jared Leto","character":"Angel Face","order":4,"cast_id":30,"profile_path":"/msugySeTCyCmlRWtyB6sMixTQYY.jpg"},{"id":7471,"name":"Zach Grenier","character":"Richard Chesler","order":5,"cast_id":31,"profile_path":"/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"},{"id":7497,"name":"Holt McCallany","character":"The Mechanic","order":6,"cast_id":32,"profile_path":"/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"},{"id":7498,"name":"Eion Bailey","character":"Ricky","order":7,"cast_id":33,"profile_path":"/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"}],"crew":[{"id":7469,"name":"Jim Uhls","department":"Writing","job":"Author","profile_path":null},{"id":7474,"name":"Ross Grayson Bell","department":"Production","job":"Producer","profile_path":null},{"id":7475,"name":"Ceán Chaffin","department":"Production","job":"Producer","profile_path":null},{"id":1254,"name":"Art Linson","department":"Production","job":"Producer","profile_path":"/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"},{"id":7477,"name":"John King","department":"Sound","job":"Original Music Composer","profile_path":null},{"id":7478,"name":"Michael Simpson","department":"Sound","job":"Original Music Composer","profile_path":null},{"id":7479,"name":"Jeff Cronenweth","department":"Camera","job":"Director of Photography","profile_path":null},{"id":7480,"name":"James Haygood","department":"Editing","job":"Editor","profile_path":null},{"id":7481,"name":"Laray Mayfield","department":"Production","job":"Casting","profile_path":null},{"id":1303,"name":"Alex McDowell","department":"Art","job":"Production Design","profile_path":null},{"id":7763,"name":"Ren Klyce","department":"Sound","job":"Sound Editor","profile_path":null},{"id":7764,"name":"Richard Hymns","department":"Sound","job":"Sound Editor","profile_path":null},{"id":7467,"name":"David Fincher","department":"Directing","job":"Director","profile_path":"/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"},{"id":7468,"name":"Chuck Palahniuk","department":"Writing","job":"Novel","profile_path":"/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"}]}}' );
alert( obj.genres.length );
alert messages showing 3, so your json response is not valid