I'm new to JSON and really struggling with this. I've read countless other posts and web pages but can't seem to figure it out.
I'm using PHP to output JSON (from data from the database) with this code:
header('Content-type: application/json');
echo json_encode($data);
Here is the JSON:
{
"x0": {
"id": "1",
"name": "Rob",
"online": "1",
"gender": "m",
"age": "29",
"height": "5'8''",
"build": "Average",
"ethnicity": "White",
"description": "Art geek person",
"looking_for": "Anything",
"image": "4fs5d43f5s4d3f544sdf.jpg",
"last_active": "29-06-11-1810",
"town": "Manchester",
"country": "UK",
"distance": 0.050973560712308
},
"x1": {
"id": "2",
"name": "Dave",
"online": "1",
"gender": "m",
"age": "29",
"height": "5'8''",
"build": "Average",
"ethnicity": "White",
"description": "Art geek person",
"looking_for": "Anything",
"image": "4fs5d43f5s4d3f544sdf.jpg",
"last_active": "29-06-11-1810",
"town": "Manchester",
"country": "UK",
"distance": 0.050973560712308
}
}
I think the problem I'm having is that the JSON is nested(might be wrong there)?.
This is the JQuery:
function fetchProfiles() {
var url='http://url.com/here';
var i = 0;
var handle = 'x'.i;
$.getJSON(url,function(json){
$.each(json.results,function(i,profile){
$("#profiles").append('<p><img src="'+profile.handle.image+'" widt="48" height="48" />'+profile.handle.name+'</p>');
i++;
});
});
}
Any ideas or suggestions appreciated!
Thanks!
i think that the problem is that you call $.each on json.results (if the json is exactly what you showed us).
you sholud do:
$.each(json,function(i,profile){
$("#profiles").append('<p><img src="'+profile.image+'" widt="48" height="48" />'+profile.name+'</p>');
});
look at the fiddle here: http://jsfiddle.net/ENcVd/1/ (it alerst the image property of you json object)
Related
from this application/ld+json i want to get example 1.genre, 2.director, 3.dateCreated, 4.duration, 5.uploadDate
<script type="application/ld+json">{
"#context": "http://schema.org",
"#type": "Movie",
"url": "/title/tt2293640/",
"name": "Minions",
"image": "https://m.media-amazon.com/images/M/MV5BMTg2MTMyMzU0M15BMl5BanBnXkFtZTgwOTU3ODk4NTE#._V1_.jpg",
"genre": [
"Animation",
"Action",
"Adventure",
"Comedy",
"Family"
],
"contentRating": "PG",
"actor": [
{
"#type": "Person",
"url": "/name/nm0000113/",
"name": "Sandra Bullock"
},
{
"#type": "Person",
"url": "/name/nm0358316/",
"name": "Jon Hamm"
},
{
"#type": "Person",
"url": "/name/nm0000474/",
"name": "Michael Keaton"
},
{
"#type": "Person",
"url": "/name/nm1853544/",
"name": "Pierre Coffin"
}
],
"director": [
{
"#type": "Person",
"url": "/name/nm0049633/",
"name": "Kyle Balda"
},
{
"#type": "Person",
"url": "/name/nm1853544/",
"name": "Pierre Coffin"
}
],
"creator": [
{
"#type": "Person",
"url": "/name/nm0528244/",
"name": "Brian Lynch"
},
{
"#type": "Organization",
"url": "/company/co0221986/"
},
{
"#type": "Organization",
"url": "/company/co0005073/"
}
],
"description": "Minions is a movie starring Sandra Bullock, Jon Hamm, and Michael Keaton. Minions Stuart, Kevin, and Bob are recruited by Scarlet Overkill, a supervillain who, alongside her inventor husband Herb, hatches a plot to take over the...",
"datePublished": "2015-06-17",
"keywords": "minion,super villain,villain,invented language,teddy bear",
"aggregateRating": {
"#type": "AggregateRating",
"ratingCount": 188589,
"bestRating": "10.0",
"worstRating": "1.0",
"ratingValue": "6.4"
},
"review": {
"#type": "Review",
"itemReviewed": {
"#type": "CreativeWork",
"url": "/title/tt2293640/"
},
"author": {
"#type": "Person",
"name": "flint1961"
},
"dateCreated": "2015-06-27",
"inLanguage": "English",
"name": "Below par money spinner",
"reviewBody": "I don\u0027t usually write reviews but this was such a disappointment that I felt I had to put something. Most of the good jokes were shown in the trailers, so if you\u0027ve seen the trailer then it\u0027s probably not worth the money. There were 3 of us watched this and although we all loved the despicable movies none of us enjoyed this. My son (11) said it felt like it was 2 hours long it was so boring. I can\u0027t really explain why it came across as so poor - but it just seemed really lazy and without Gru to play against the Minnions were just to simple. It was slapstick with no soul. The music although quite good didn\u0027t really seem to fit the film - just seemed to be tacked on at bit were they felt they should have a soundtrack and a famous song. Buy yourself a copy of despicable me and watch it again and again. Much better value for money! This just felt rushed, with loads of stock jokes, maybe hoping enough of them would hit the mark - they didn\u0027t.",
"reviewRating": {
"#type": "Rating",
"worstRating": "1",
"bestRating": "10",
"ratingValue": "4"
}
},
"duration": "PT1H31M",
"trailer": {
"#type": "VideoObject",
"name": "Stuart \u0026 Dave",
"embedUrl": "/video/imdb/vi996454425",
"thumbnail": {
"#type": "ImageObject",
"contentUrl": "https://m.media-amazon.com/images/M/MV5BMTgxMzc2OTQwOV5BMl5BanBnXkFtZTgwODQ2OTgwNjE#._V1_.jpg"
},
"thumbnailUrl": "https://m.media-amazon.com/images/M/MV5BMTgxMzc2OTQwOV5BMl5BanBnXkFtZTgwODQ2OTgwNjE#._V1_.jpg",
"description": "Watch an exclusive video for Minions.",
"uploadDate": "2015-06-26T17:14:58Z"
}
}</script>
to get meta tag im using this code
$c = file_get_contents("https://www.imdb.com/title/$id");
$d = new DomDocument();
$d->loadHTML($c);
$xp = new domxpath($d);
foreach ($xp->query("//meta[#property='og:title']") as $ti) {
$imdbTitle = $ti->getAttribute("content");
}
foreach ($xp->query("//meta[#property='og:image']") as $im) {
$imdbImg = $im->getAttribute("content");
}
foreach ($xp->query("//meta[#property='og:description']") as $ode) {
$imdbDes = $ode->getAttribute("content");
}
from this php code I want to get and 1.genre, 2.director, 3.dateCreated, 4.duration, 5.uploadDate from JSON (application/ld+json) how i can do this? some help thanks you.
You have a JSON data into a HTML page.
So you have to
1) retrieve the JSON data only
2) parse the JSON and process it
$id = 'tt0159365' ; // example id
$c = file_get_contents("https://www.imdb.com/title/$id");
// retrieve the JSON data
$d = new DomDocument();
#$d->loadHTML($c);
// parse the HTML to retrieve the "ld+json" only
$xp = new domxpath($d);
$jsonScripts = $xp->query( '//script[#type="application/ld+json"]' );
$json = trim( $jsonScripts->item(0)->nodeValue ); // get the first script only (it should be unique anyway)
// decode the JSON string we find into a associative array
$data = json_decode( $json, true );
// you can now use this array to query the data you want
$duration = $data['duration'];
echo "Duration: $duration" ;
$director = $data['director']['name'] ;
echo "Director : $director" ;
I am making a login app in ionic using PHP, I have a login function which works well but I need to be able to store certain values to local storage for later use.
Here is a sample from my code:
$scope.login = function() {
Login.login($scope.login).then(function (data) {
if (Object.keys(data.data).length === 1) {
var datao = angular.toJson(data);
$scope.profile = datao;
$scope.email = $scope.profile.email;
$scope.username = $scope.profile.username;
console.log(data);
console.log($scope.profile);
// $state.go('dashboard');
}
....
});
....
});
$scope.profile displays the following:
[Log]
{
"data": [{
"id": "5",
"username": "ozombo",
"reputation": "ceo",
"activate": "8425",
"followercount": "3",
"praycount": "0",
"donetour": "0",
"fb_id": "0",
"fullname": "Adebambo Oyelaja",
"church": "RCCG ",
"photo": "1417451697.jpg",
"twtr_id": "0",
"screen_name": "",
"email": "oyexs911#yahoo.com",
"bio": "Geek, Father, Husband",
"country": "Nigeria",
"state": "Lagos",
"followscount": "0",
"account": "",
"password": "5cf1bc1b9a2ada1ae9e29079aae1aefa",
"signup_date": "1413305984",
"signupdevice": "web",
"keycode": "5fc868f0767b0c164341a9e8e35edbe4",
"last_login": "1436519269",
"city": "Palmgroove",
"campaign": "",
"bday": "29-09-1988",
"gender": "Male",
"approved": "0",
"donewelcome": "0",
"phonenumber": "07017993683",
"secure": "private",
"churchid": "1",
"views": "68",
"marital": "Married"
}],
"status": 200,
"config": {
"method": "GET",
"transformRequest": [null],
"transformResponse": [null],
"url": "http://localhost:8888/opb/api/login.php?user=oyexs911#yahoo.com&pass=oyelaja",
"headers": {
"Accept": "application/json, text/plain, /"
}
},
"statusText": "OK"
}
How will I get the user email for example
You can get the email by accesing to data.email like var email = $scope.profile.data.email
You forgot that you have data before you can get the email.
{
"data": [{
"username": "ozombo",
"email": "oyexs911#yahoo.com",
...
}]
}
You can get access to the user email by iterating over the data array:
$scope.profile.data.forEach(function(d) {
console.log(d.email);
});
Hope that helps!
Bellow JSON contain my fan page info in the json format.I tried getJson in the jquery but it didn't work for me
{
"about": " One who does something \n\n",
"description": "Doers Web development\nDoers Software Development\nDoers Graphics design\nDoers Hardware & Networking\nDoers Mobile Application Development",
"is_published": true,
"location": {
"street": "",
"city": "Colombo",
"country": "Sri Lanka",
"zip": ""
},
"phone": "+94773633412",
"talking_about_count": 686,
"username": "DoersIncorporation",
"website": "http://www.doers.lk",
"were_here_count": 0,
"category": "Computers/technology",
"id": "397319800348866",
"name": "Doers Inc",
"link": "https://www.facebook.com/DoersIncorporation",
"likes": 767,
"cover": {
"cover_id": 398352273578952,
"source": "http://sphotos-f.ak.fbcdn.net/hphotos-ak-snc7/s720x720/484190_398352273578952_36222530_n.png",
"offset_y": 0
}
}
What i want to know is how to parse cover image from it using php and jquery ???
Save the JSON to a variable:
var x = theJSON
x.cover.source
will return the url
Fiddle
I'm working with some videos on PHP, using zencoder to encode the videos, save them on s3 and then notify my site back when it's all done.
Everything is working until I have to process the notifications returned as json and pull out the new url to the saved video.
this:
$notification = $zencoder->notifications->parseIncoming();
if($notification->job->state == "finished")
{
$encode_id=$notification->job->id;
}
works fine. I just need some pointers on accessing the url.
The notification is sent as:
{
"output": {
"frame_rate": 30.0,
"label": "video_id_",
"total_bitrate_in_kbps": 3115,
"md5_checksum": null,
"channels": "2",
"audio_codec": "aac",
"duration_in_ms": 4225,
"video_codec": "h264",
"url": "http://my_url/597bd3592bf4a9d70f04dc676c44de6d.mp4",
"thumbnails": [{
"label": null,
"images": [{
"url": "http://my_url/_key__0000.png",
"format": "PNG",
"dimensions": "640x360",
"file_size_bytes": 482422
}]
}],
"video_bitrate_in_kbps": 3052,
"width": 640,
"format": "mpeg4",
"height": 360,
"audio_sample_rate": 44100,
"state": "finished",
"audio_bitrate_in_kbps": 63,
"id": 41424918,
"file_size_in_bytes": 1625847
},
"input": {
"frame_rate": 30.0,
"total_bitrate_in_kbps": 3867,
"md5_checksum": null,
"channels": "2",
"audio_codec": "aac",
"duration_in_ms": 4167,
"video_codec": "h264",
"video_bitrate_in_kbps": 3764,
"width": 640,
"format": "mpeg4",
"height": 360,
"audio_sample_rate": 44100,
"state": "finished",
"audio_bitrate_in_kbps": 103,
"id": 22371764,
"file_size_in_bytes": 2028809
},
"job": {
"created_at": "2012-07-14T22:25:08Z",
"test": true,
"updated_at": "2012-07-14T22:25:47Z",
"submitted_at": "2012-07-14T22:25:08Z",
"pass_through": null,
"state": "finished",
"id": 22377083
}
}
but something like: $video_file=$notification->output->url; doesn't.
What am I missing?
If you don't want to use the parseIncoming method...
Use:
$notification = json_decode(trim(file_get_contents('php://input')), true);
as apposed to:
$notification = $zencoder->notifications->parseIncoming();
The second paramater of 'true' will format the results as an array as apposed to an object. From there, you can access all the values like:
$notification['output']['file_size_in_bytes'];
The parseIncoming method will return a stdClass, so referencing values within it is done with:
$notification->key
This question already has answers here:
json_decode to array
(12 answers)
Closed 11 months ago.
I want to have only review from this JSON.
var json = {
"provider": {
"id": "1982660171",
"display_name": "Stephen R Guy, MD",
"last_name": "Guy",
"first_name": "Stephen",
"middle_name": "Russell",
"master_name": "Stephen_Guy",
"degree_types": "MD",
"familiar_name": "Stephen",
"years_experience": "27",
"birth_year": "1956",
"birth_month": "5",
"birth_day": "23",
"gender": "M",
"is_limited": "false",
"url_deep": "http:\/\/www.vitals.com\/doctor\/profile\/1982660171\/Stephen_Guy",
"url_public": "http:\/\/www.vitals.com\/doctors\/Dr_Stephen_Guy.html",
"status_code": "A",
"client_ids": "1",
"quality_indicator_set": [{
"type": "quality-indicator\/consumer-feedback",
"count": "2",
"suboverall_set": [{
"name_short": "Promptness",
"overall": "3"
}, {
"name_short": "Courteous Staff",
"overall": "4"
}, {
"name_short": "Bedside Manner",
"overall": "4"
}, {
"name_short": "Spends Time with Me",
"overall": "4"
}, {
"name_short": "Follow Up",
"overall": "4"
}],
"name": "Consumer Reviews",
"overall": "4.0",
"measure_set": [{
"feedback_response_id": "1756185",
"input_source_ids": "{0}",
"date": "1301544000",
"value": "4",
"scale": {
"best": "1",
"worst": "4"
},
"review": {
"type": "review\/consumer",
"comment": "I will never birth with another dr. Granted that's not saying much as I don't like dr's but I actually find him as valuable as the midwives who I adore. I liked Horlacher but when Kitty left I followed the midwives and then followed again....Dr. Guy is GREAT. I honestly don't know who I'd rather support me at my birth; Margie and Lisa or Dr. Guy. ....I wonder if I can just get all of them.Guy's great. Know what you want. Tell him. Be strong and he'll support you.I give him 10 stars. Oh...my baby's 3 years old now. He's GREAT! ",
"date": "1301544000"
},
"sub_measure": [{
"name": "Waiting time during a visit",
"name_short": "Promptness",
"value": "3",
"scale": {
"best": "4",
"worst": "1"
}
}, {
"name": "Courtesy and professionalism of office staff ",
"name_short": "Courteous Staff",
"value": "4",
"scale": {
"best": "4",
"worst": "1"
}
}, {
"name": "Bedside manner (caring)",
"name_short": "Bedside Manner",
"value": "4",
"scale": {
"best": "4",
"worst": "1"
}
}, {
"name": "Spending enough time with me",
"name_short": "Spends Time with Me",
"value": "4",
"scale": {
"best": "4",
"worst": "1"
}
}, {
"name": "Following up as needed after my visit",
"name_short": "Follow Up",
"value": "4",
"scale": {
"best": "4",
"worst": "1"
}
}]
}, {
"feedback_response_id": "420734",
"input_source_ids": "{76}",
"link": "http:\/\/local.yahoo.com\/info-15826842-guy-stephen-r-md-university-women-s-health-center-dayton",
"date": "1142398800",
"value": "4",
"scale": {
"best": "1",
"worst": "4"
},
"review": {
"type": "review\/consumer",
"comment": "Excellent Doctor: I really like going to this office. They are truely down to earth people and talk my \"non-medical\" language. I have been using thier office since 1997 and they have seen me through 2 premature pregnancies!",
"date": "1142398800"
}
}],
"wait_time": "50"
}]
}
};
Use json_decode (req. PHP 5.2) to get a PHP array. Then you can walk the array for the values you need.
Docs: http://php.net/manual/en/function.json-decode.php
<?php
//Note I changed the json a bit to make it parsable
$json = '{ "provider":{ "id":"1982660171", "display_name":"Stephen R Guy, MD", "last_name":"Guy", "first_name":"Stephen", "middle_name":"Russell", "master_name":"Stephen_Guy", "degree_types":"MD", "familiar_name":"Stephen", "years_experience":"27", "birth_year":"1956", "birth_month":"5", "birth_day":"23", "gender":"M", "is_limited":"false", "url_deep":"http:\\/\\/www.vitals.com\\/doctor\\/profile\\/1982660171\\/Stephen_Guy", "url_public":"http:\\/\\/www.vitals.com\\/doctors\\/Dr_Stephen_Guy.html", "status_code":"A", "client_ids":"1", "quality_indicator_set":[ { "type":"quality-indicator\\/consumer-feedback", "count":"2", "suboverall_set":[ { "name_short":"Promptness", "overall":"3" }, { "name_short":"Courteous Staff", "overall":"4" }, { "name_short":"Bedside Manner", "overall":"4" }, { "name_short":"Spends Time with Me", "overall":"4" }, { "name_short":"Follow Up", "overall":"4" } ], "name":"Consumer Reviews", "overall":"4.0", "measure_set":[ { "feedback_response_id":"1756185", "input_source_ids":"{0}", "date":"1301544000", "value":"4", "scale":{ "best":"1", "worst":"4" }, "review":{ "type":"review\\/consumer", "comment":"I will never birth with another dr. Granted that\'s not saying much as I don\'t like dr\'s but I actually find him as valuable as the midwives who I adore. I liked Horlacher but when Kitty left I followed the midwives and then followed again....Dr. Guy is GREAT. I honestly don\'t know who I\'d rather support me at my birth; Margie and Lisa or Dr. Guy. ....I wonder if I can just get all of them.Guy\'s great. Know what you want. Tell him. Be strong and he\'ll support you.I give him 10 stars. Oh...my baby\'s 3 years old now. He\'s GREAT! ", "date":"1301544000" }, "sub_measure":[ { "name":"Waiting time during a visit", "name_short":"Promptness", "value":"3", "scale":{ "best":"4", "worst":"1" } }, { "name":"Courtesy and professionalism of office staff ", "name_short":"Courteous Staff", "value":"4", "scale":{ "best":"4", "worst":"1" } }, { "name":"Bedside manner (caring)", "name_short":"Bedside Manner", "value":"4", "scale":{ "best":"4", "worst":"1" } }, { "name":"Spending enough time with me", "name_short":"Spends Time with Me", "value":"4", "scale":{ "best":"4", "worst":"1" } }, { "name":"Following up as needed after my visit", "name_short":"Follow Up", "value":"4", "scale":{ "best":"4", "worst":"1" } } ] }, { "feedback_response_id":"420734", "input_source_ids":"{76}", "link":"http:\\/\\/local.yahoo.com\\/info-15826842-guy-stephen-r-md-university-women-s-health-center-dayton", "date":"1142398800", "value":"4", "scale":{ "best":"1", "worst":"4" }, "review":{ "type":"review\\/consumer", "comment":"Excellent Doctor: I really like going to this office. They are truely down to earth people and talk my \\"non-medical\\" language. I have been using thier office since 1997 and they have seen me through 2 premature pregnancies!", "date":"1142398800" } } ], "wait_time":"50" } ] } } ';
$obj = json_decode($json);
//This is how you can retrieve data
print $obj->{'provider'}->{'id'};
?>
Use the json_decode function.