I am trying to query and parse Yahoo fantasy sports data and show results in a friendly format.
Here is the JSON I get back from Yahoo from a successful request...
{
"fantasy_content": {
"xml:lang": "en-US",
"yahoo:uri": "\/fantasy\/v2\/users;use_login=1\/teams",
"users": {
"0": {
"user": [
{
"guid": "1234567890"
},
{
"teams": {
"0": {
"team": {
"team_key": "268.l.auto.t.209996",
"team_id": "209996",
"name": "Team Test",
"type": "auto"
}
},
"1": {
"team": {
"team_key": "273.l.auto.t.27741",
"team_id": "27741",
"name": "Team API",
"type": "auto"
}
},
"count": 2
}
}
]
},
"count": 1
},
"time": "29.808044433594ms",
"copyright": "Data provided by Yahoo! and STATS, LLC",
"refresh_rate": false
}
}
I am looking to get the team names from the teams array. I have tried using the following PHP code (among countless variations of it) to drill to the node I need but I am not having any luck.
$obj=json_decode($json);
$data = $obj->fantasy_content->users->user->teams->team;
foreach($data as $d){
echo 'name: ' . $d->name ; //prints php
}
I am hoping that someone might be able to provide a working example using the JSON above as I have clearly failed at this. Any help would be greatly appreciated.
Thanks in advance!
Try this
$obj=json_decode($json);
$data = $obj->fantasy_content->users->{'0'}->user[1]->teams;
foreach($data as $d){
echo 'name: ' . $d->team->name ; //prints php
}
EDIT
you can use the cout to loop through the names
$obj=json_decode($json);
$data = $obj->fantasy_content->users->{'0'}->user[1]->teams;
for($i = 0; $i < $data->count; $i++){
echo 'name: ' . $data->{$i}->team->name ; //prints php
}
Related
I have multiple JSON files with different structures. What I want to do is to automatically display these JSON outputs with HTML.
Some of my JSON outputs are as follows: (Think of each of these as separate files and need to be processed separately)
{
"parts": [
{
"#attributes": {
"id": "part1"
},
"car": "Peugeot",
"service": 5,
"location": 2996,
"price": "44.95",
"date": "2000-10-01"
},
... other objects
]
}
{
"licenses":[
{
"driver":"John",
"year":26,
"info":null
},
... other objects
]
}
Now, to process these files, I send the page name with GET on PHP and I want the corresponding JSON output to be printed to the screen with HTML as <span>$key</span> -> <span>$value</span>
How can I make this dynamic JSON output read event with PHP? Do I need to create a recursive function?
Because the files have different structures from each other. I hope I was able to explain my problem. Thanks already for yours help.
I suggest the following:
get required JSON file name from GET or POST, for example:
$jsonfilename = $_GET['file'];
The above does not include any security protection! this is a separate topic,
so do some research.
load your json file and parse it:
$json = file_get_contents('/path/'.$jsonfilename);
$data = json_decode($json, true);
read your json data:
foreach ($data as $key=>$value){
echo ''.$key.' -> '.$value.'';
}
A simple example for your PARTS file:
$json = '
{
"parts":
[
{
"#attributes": {
"id": "part1"
},
"car": "Peugeot",
"service": 5,
"location": 2996,
"price": "44.95",
"date": "2000-10-01"
},
{
"#attributes": {
"id": "part2"
},
"car": "Renault",
"service": 8,
"location": 3100,
"price": "99.95",
"date": "2022-03-01"
}
]
}';
$arr = json_decode($json, true);
foreach($arr["parts"] as $part) {
foreach($part as $k => $v){
if($k == "#attributes")
echo "<h1>" . $v["id"] ."</h1>";
else
echo "<span>$k</span> -> <span>$v</span> <br/>";
}
}
This produces:
I want to get id,url,img and title of videos in the JSON data.My current code doesn't output anything
could any one tell me what i am doing wrong.Thanks
$code2 = stripslashes($_POST['outputtext']);
$data = json_decode($code2, true);
$i = 0;
foreach($data->videos as $values)
{
echo $values->id . "\n";
echo $values->url . "\n";
echo $values->img . "\n";
echo $values->title . "\n";
$i++;
}
data:
{
"cat": {
"id": "1234567",
"source_id": null,
"title_en": "first season",
"description_en": "This is spring category ",
},
"videos": [{
"id": "312412343",
"url": "\/2015-07-17\/1abcd.mp4",
"img": "image\/44\/\/2015-07-17\/1abcd.jpg",
"title": "first",
}, {
"id": "2342343",
"url": "\/2015-07-16\/2dcdeg.mp4",
"img": "images\/44\/\/2015-07-16\/2dcdeg.jpg",
"title": "second",
}];
}
validated json data:
{
"cat":{
"id":"1234567",
"source_id":null,
"title_en":"first season",
"description_en":"This is spring category "
},
"videos":[
{
"id":"312412343",
"url":"\/2015-07-17\/1abcd.mp4",
"img":"image\/44\/\/2015-07-17\/1abcd.jpg",
"title":"first"
},
{
"id":"2342343",
"url":"\/2015-07-16\/2dcdeg.mp4",
"img":"images\/44\/\/2015-07-16\/2dcdeg.jpg",
"title":"second"
}
]
}
Because you call
$data = json_decode($code2, true);
your $data is an array and not an object as you try to access it. So either access it as regular array, or change 2nd argument of json_decode() to false (or removeit as this is default), as this is what controls conversion behavior.
See docs: http://php.net/manual/en/function.json-decode.php
Your data is not a valid JSON (; at the end, redundant and missing comas - it's simply broken).
In case of such problems, var_dump() is pretty helpful to inspect what data you are really working with.
I need to decode a json file and display it in the page..
My code is not working, and i don't know whats wrong.
The output is just a blank page.
Here is the json file..
{
"data": [
{
"name": "Jhaimee",
"uid": 10000
},
{
"name": "Ally",
"uid": 10000133
},
{
"name": "Macers",
"uid": 1000056
},
{
"name": "Diego",
"uid": 100004
},
{
"name": "Killersmile",
"uid": 1000050
},
{
"name": "Joel",
"uid": 1000011
}
]
}
I want the output to be..
Jhaimee
Ally
Macers
Diego
Killersmile
Joel
Here is my php.
$data = file_get_contents("https://graph.facebook.com/fql?q=SELECT name,uid FROM user WHERE uid IN (SELECT recipients FROM thread WHERE folder_id = 0 ORDER BY message_count DESC) AND uid != me() LIMIT 10&access_token=xxxx");
$data = json_decode($data, true);
echo $data[0]["name"];
The outermost array key should be data
echo $data['data'][0]['name']
foreach($data['data'] as $person){ echo $person['name'] . "<br />\n"; }
Rather new to using json with php, bit of a last resort having searched over the net quite a bit already. I have this example ison file below, which I wish to be able to echo the total number of 'name:' fields within it so eg. 4 in the example below.
Question: How would I go about doing this?
[
{
"age": "22",
"name": "Dave"
},
{
"age": "21",
"name": "Alan"
},
{
"age": "19",
"name": "Luke"
},
{
"age": "30",
"name": "Nina"
}
]
If there will always be a name in each array then just:
echo count(json_decode($json, true));
If name may or not be present in each array then:
PHP >= 5.5.0:
echo count(array_column(json_decode($json, true), 'name'));
PHP < 5.5.0:
$count = 0;
foreach(json_decode($json, true) as $k => $v) {
isset($v['name']) ? $count++ : $count;
}
echo $count;
I have a TXT file containing some facebook Open Graph info like this:
{
"data": [
{
"name": "Avatar",
"category": "Movie",
"id": "82771544063",
"created_time": "2012-04-13T21:16:56+0000"
},
{
"name": "HappyDance",
"category": "Movie",
"id": "243564344063",
"created_time": "2012-04-13T21:16:56+0000"
}
],
"paging": {
"next": "https://graph.facebook.com/me/likes?format=json&limit=5000&offset=5000&__after_id=5546653546361"
}
}
In PHP, I want to extract all the id numbers from the rows that show
"id": "XXXXXXXXXXXX",
The output should look like this:
I like 8277564344063
I like 243564344063
I started the following but I am getting an error:
<?php
$file_handle = fopen("raw.txt", "rb");
ob_start();
$text = file_get_contents('raw.txt');
$decode = json_decode($text);
print_r($decode);
$new_content = ob_get_clean();
file_put_contents("likes.txt", $new_content);
fclose($file_handle);
?>
The error is that my output is blank! What am I doing wrong?
Please help?
You don't have valid JSON.
The JSON Object below this line is valid JSON. I removed the comma after your last associative array within your "data" array. You shouldn't need a comma at the end of the array.
{
"data": [
{
"name": "Avatar",
"category": "Movie",
"id": "82771544063",
"created_time": "2012-04-13T21:16:56+0000"
},
{
"name": "HappyDance",
"category": "Movie",
"id": "243564344063",
"created_time": "2012-04-13T21:16:56+0000"
}
],
"paging": {
"next": "https://graph.facebook.com/me/likes? format=json&limit=5000&offset=5000&__after_id=5546653546361"
}
}
Parse error on line 14:
... }, ], "paging": {
---------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
As I removed the comma from the non-valid JSON. I was able to get the result you wanted.
<?php
$json_object = file_get_contents('fb.json');
if(!$json_object) {
echo "oops, cant read the file";
}
// remap json_object
$json_object = json_decode($json_object,true);
foreach($json_object['data'] as $item) {
$items[] = "I like" . ' ' . $item['id'];
/* If you want to just echo " I like xyz" etc
* use echo "I like" . $item['id'];
*/
}
$list = implode(',',$items);
echo $list;
?>