Loop through json (non-object ?) - php

I want to show a list of TV programs with title, genre, subgenre, description, start time and duration using a json file on server but I get the following errors:
E_NOTICE : type 8 -- Trying to get property of non-object -- at line 13
E_WARNING : type 2 -- Invalid argument supplied for foreach() -- at line 13
Here is a piece of a sample json:
{"channel":"6280",
"banned":true,
"plan":[
{"id":"-1",
"pid":"0",
"starttime":"00:00",
"dur":"65",
"title":"",
"normalizedtitle": "",
"desc":"",
"genre":"",
"subgenre":"",
"prima":false
},
{"id":"94622386",
"pid":"507461",
"starttime":"01:05",
"dur":"65",
"title":"Sex Researchers",
"normalizedtitle": "sex-researchers",
"desc":"Ep. 2 - Ciclo The Body of...",
"genre":"mondo e tendenze",
"subgenre":"societa",
"prima":false
},
Here is the php code that I use:
<?php
$channel = '6280';
$current_unix = time();
$json = json_decode(file_get_contents('http://guidatv.sky.it/app/guidatv/contenuti/data/grid/'.date('y_m_d').'/ch_'.$channel.'.js'));
//print_r($json);
echo '<ul>';
foreach ($json as $data) {
echo '<li>';
foreach ($data->plan as $prog) {
if ( $current_unix < $prog->starttime ) {
echo $prog->id . '<br>';
echo $prog->starttime . '<br>';
echo $prog->dur . '<br>';
echo $prog->desc . '<br>';
if ( isset($prog->genre)) {
echo $prog->genre . '<br>';
}
}
}
echo '</li>';
}
echo '</ul>';
?>
Could you help me to solve this problem? Thank you

You cannot loop on the object. Your JSON does not return the array of channel, but return only a channel object. Here is what you want:
$json = json_decode(file_get_contents('http://guidatv.sky.it/app/guidatv/contenuti/data/grid/'.date('y_m_d').'/ch_'.$channel.'.js'));
echo '<ul>';
foreach ($json->plan as $prog) {
echo "<li>" . $prog->title . '</li>';
}
echo '</ul>';

Related

How do I get one json value value each?

in this my php code
echo $_POST[result];
in this my php result
{
"result_code":0,
"err_cd":"",
"result_msg":"",
"store_id":"M20C2685",
"status":"APPROVED",
"order_no":"600a2a044c9be",
"tr_no":725,
"tr_price":1000,
"pay_price":1000,
"approved_day":"20210122",
"approved_time":"102744",
"param1":"",
"param2":""
}
I want to print out each value one by one. What should I do?
json_decode Parsing JSON
<?php
$str = '{"result_code":0,
"err_cd":"",
"result_msg":"",
"store_id":"M20C2685",
"status":"APPROVED",
"order_no":"600a2a044c9be",
"tr_no":725,
"tr_price":1000,
"pay_price":1000,
"approved_day":"20210122",
"approved_time":"102744",
"param1":"",
"param2":""
}';
$arr = json_decode($str, true);
echo $arr['err_cd'] . PHP_EOL;
echo $arr['result_msg'] . PHP_EOL;
echo $arr['store_id'] . PHP_EOL;
echo $arr['status'] . PHP_EOL;
// ......
// Circulates each value in the array
foreach ($arr as $item) {
echo $item . PHP_EOL;
}

How do i display list items from php array

Ive been trying make this display as html list items it just a string that i explode then loop over each item i cant get it to out put correctly. Could some one please show me where im going wrong or suggest an new approch.
this is what ive tried
$path = "1/2/3/4";
$expath = explode("/",$path);
$ret = '';
echo '<ul>';
foreach ($expath as $pitem) {
echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
}
echo '</ul>';
.
Desired out put on hrefs
1
1/2
1/2/3
1/2/3/4
Desired visual out LIs
1
2
3
4
Output i get be warned
1
12/>212/>23/>312/>23/>34/>4
$path = "1/2/3/4";
$expath = explode("/", $path);
echo '<ul>';
foreach ($expath as $i => $pitem) {
$slice = array_slice($expath, 0, $i + 1);
$path = implode('/', $slice);
echo '<li>' . $pitem . '</li>';
}
echo '</ul>';
$list = explode("/", "1/2/3/4");
This will create an array $list as:
echo $list[0]; // 1
echo $list[1]; // 2
echo $list[2]; // 3
echo $list[3]; // 4
This line is the problem: echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
Should be formatted like:
echo "<li><a href='{$ret}={$pitem}/'>{$pitem}</a></li>";
or echo '<li>'.$pitem.'</li>';
Its because your $ret. Place that inside the loop. In your code you concatenate $pitem with $ret all older $ret values also get concat.
Try
<?php
$path = "1/2/3/4";
$expath = explode("/",$path);
echo '<ul>';
foreach ($expath as $pitem) {
$ret = '';
echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
}
echo '</ul>';
If you want = sign tobe there in the url then just change echo by following
echo "<li><a href='$ret=$pitem/'>$pitem</a></li>";
PHP echo with double quotes will print variable value.

Parsing pinterest JSON api with PHP

Hi im having problem parsing this pinterest JSON file, any ideas? thanks
$json = file_get_contents('http://pinterestapi.co.uk/jwmoz/boards');
$obj = json_decode($json);
foreach($obj->body as $item){
$example = $item[0]->name;
echo $example;
}
{
"body":[
{"name":"JMOZ",
"href":"http:\/\/pinterest.com\/jwmoz\/jmoz\/",
"num_of_pins":17,"cover_src":"http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920643849_2DcDfCUK_222.jpg",
"thumbs_src":
["http:\/\/media-cache-ec5.pinterest.com\/upload\/82190761920643841_dZfvCWmE_t.jpg",
"http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920194573_aPAbDtHD_t.jpg",
"http:\/\/media-cache-ec2.pinterest.com\/upload\/82190761920194563_dQcOIHvQ_t.jpg",
"http:\/\/media-cache0.pinterest.com\/upload\/82190761920194557_VSSI2uQB_t.jpg"
]
},
{"name":"JMOZ",
"href":"http:\/\/pinterest.com\/jwmoz\/jmoz\/",
"num_of_pins":17,"cover_src":"http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920643849_2DcDfCUK_222.jpg",
"thumbs_src":
["http:\/\/media-cache-ec5.pinterest.com\/upload\/82190761920643841_dZfvCWmE_t.jpg",
"http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920194573_aPAbDtHD_t.jpg",
"http:\/\/media-cache-ec2.pinterest.com\/upload\/82190761920194563_dQcOIHvQ_t.jpg",
"http:\/\/media-cache0.pinterest.com\/upload\/82190761920194557_VSSI2uQB_t.jpg"
]
},
{"name":"JMOZ",
"href":"http:\/\/pinterest.com\/jwmoz\/jmoz\/",
"num_of_pins":17,"cover_src":"http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920643849_2DcDfCUK_222.jpg",
"thumbs_src":
["http:\/\/media-cache-ec5.pinterest.com\/upload\/82190761920643841_dZfvCWmE_t.jpg",
"http:\/\/media-cache-ec4.pinterest.com\/upload\/82190761920194573_aPAbDtHD_t.jpg",
"http:\/\/media-cache-ec2.pinterest.com\/upload\/82190761920194563_dQcOIHvQ_t.jpg",
"http:\/\/media-cache0.pinterest.com\/upload\/82190761920194557_VSSI2uQB_t.jpg"
]
},
{"name":"test I\u00f1t\u00ebrn\u00e2ti\u00f4n\u00e0liz\u00e6ti\u00f8n",
"href":"http:\/\/pinterest.com\/jwmoz\/test-internationaliztin\/",
"num_of_pins":0,
"cover_src":false,
"thumbs_src":false
}],
"meta":{"count":11}
}
It seems the problem is in your usage of the [0] index on $item
$example = $item[0]->name;
This should just be
$example = $item->name;
To access the thumbnails, try
$obj = json_decode($json);
foreach($obj->body as $item){
echo '<li>' . $item->name . '<ul>';
if(!empty($item->thumbs_src))
{
foreach($item->thumbs_src as $thumbs_src){
echo '<li>' . $thumbs_src . '</li>';
}
}
echo '</ul></li>';
}

JSON Fatal Error foreach

I´ve got the following:
include('php/get_recipe_byID.php');
$jsonstring1 = $recipe_byidarr;
$recip = json_decode($recipe_byidarr, true);
print_r($recip);
foreach ($recip['Data']['Recipes'] as $key => $newrecipe) {
// echo '<li>
// <a href="/recipe_search.php?id=' . $recipe['ID'] . '">';
echo 'seas';
echo $newrecipe['TITLE'];
echo '<br><br>';
}
When I call it in the browser, it tells me that
Fatal error: Cannot use string offset as an array in /var/www/recipe_search.php on line 43
This is the line of the foreach loop.
$recip is the following:
{"Data":{"Recipes":{"Recipe_9":{"ID":"9","TITLE":"Schnitzel","TEXT":"Alex\u00b4s Hausmannskost","COUNT_PERSONS":"4","DURATION":"40","USER_ID":"1","DATE":"2011-09-16 00:00:00"}}},"Message":null,"Code":200}
Do anybody know where my mistake is?
Just try it:
$taskSeries = $array['Data']['Recipes']['Recipe_'.$_GET['id']];
if(array_key_exists('TITLE', $taskSeries)) {
$taskSeries = array($taskSeries);
}
foreach($taskSeries as $task) {
$title = $task['TITLE'];
// do something with $title and other
}

Parse JSON data created by php json_encode

I want to be able to parse the following json data. It was constructed from a php array using jsonencode. I've added the json below to help you understand it. I'd like to be able to display the json in a bulleted form. It show two records with associated category array and tags array. Im open to using any libraries to help.
{"0":{"categories":[{"name":"Football Club","slug":"football-club"}],"tags":[{"name":"England","slug":"england"},{"name":"EPL","slug":"epl"},{"name":"Europe","slug":"europe"},{"name":"Champions","slug":"champions"}],"ID":"908","post_author":"78350","post_date":"2010-10-18 10:49:16","post_title":"Liverpool Football Club","post_content":"Content goes here...","post_name":"liverpoolfc","guid":"http://www.liverpoolfc.tv","post_type":"post","comment_count":"0","comment_status":"open","relevance_count":0},"1":{"categories":[{"name":"Football Club","slug":"football-club"}],"tags":[{"name":"England","slug":"england"},{"name":"EPL","slug":"epl"},{"name":"Europe","slug":"europe"},{"name":"Champions","slug":"champions"}],"ID":"907","post_author":"78350","post_date":"2010-10-18 10:49:16","post_title":"Everton Football Club","post_content":"Content goes here","post_name":"evertonfc","guid":"http://www.evertonfc.tv","post_type":"post","comment_count":"0","comment_status":"open","relevance_count":0}}
I want to be able to parse it and display like this.
Liverpool Football Club
Content goes
here
Categories
Football Club
Tags
England
EPL
UPDATE: Sorry i need to parse it in javascript.
Try this:
$json = '{"0":{"categories":[{"name":"Football Club","slug":"football-club"}],"tags":[{"name":"England","slug":"england"},{"name":"EPL","slug":"epl"},{"name":"Europe","slug":"europe"},{"name":"Champions","slug":"champions"}],"ID":"908","post_author":"78350","post_date":"2010-10-18 10:49:16","post_title":"Liverpool Football Club","post_content":"Content goes here...","post_name":"liverpoolfc","guid":"http://www.liverpoolfc.tv","post_type":"post","comment_count":"0","comment_status":"open","relevance_count":0},"1":{"categories":[{"name":"Football Club","slug":"football-club"}],"tags":[{"name":"England","slug":"england"},{"name":"EPL","slug":"epl"},{"name":"Europe","slug":"europe"},{"name":"Champions","slug":"champions"}],"ID":"907","post_author":"78350","post_date":"2010-10-18 10:49:16","post_title":"Everton Football Club","post_content":"Content goes here","post_name":"evertonfc","guid":"http://www.evertonfc.tv","post_type":"post","comment_count":"0","comment_status":"open","relevance_count":0}}';
$array = json_decode($json, true);
foreach ($array as $item) {
echo '<ul>' . PHP_EOL;
echo '<li>' . $item['post_title'] . '</li>' . PHP_EOL;
echo '<li>' . $item['post_content'] . '</li>' . PHP_EOL;
/* Display Categories */
echo '<li>Categories' . PHP_EOL;
echo '<ul>' . PHP_EOL;
if (!empty($item['categories'])) {
foreach ($item['categories'] as $category) {
echo '<li>' . $category['name'] . '</li>' . PHP_EOL;
}
} else {
echo '<li>No Categories Available</li>' . PHP_EOL;
}
echo '</ul>' . PHP_EOL;
echo '</li>' . PHP_EOL;
/* Display Tags */
echo '<li>Tags' . PHP_EOL;
echo '<ul>' . PHP_EOL;
if (!empty($item['tags'])) {
foreach ($item['tags'] as $tag) {
echo '<li>' . $tag['name'] . '</li>' . PHP_EOL;
}
} else {
echo '<li>No Tags Available</li>' . PHP_EOL;
}
echo '</ul>' . PHP_EOL;
echo '</li>' . PHP_EOL;
echo '</ul>' . PHP_EOL;
}
UPDATE Are you asking on how to do this in PHP or in Javascript/jQuery? You didn't quite explain what you were doing with it.
UPDATE Here it is using Javascript/jQuery: http://jsfiddle.net/wgjjR/
//<div id="container"></div>
//var json = {}; // this is your JSON object
var container = $('#container'), html = [];
for (var key in json) {
var item = json[key];
html.push('<ul>');
html.push('<li>' + item.post_title + '</li>');
html.push('<li>' + item.post_content + '</li>');
html.push('<li>Categories<ul>');
for (var cat in item.categories) {
cat = item.categories[cat];
html.push('<li>' + cat.name + '</li>');
}
html.push('</ul></li>');
html.push('<li>Tags<ul>');
for (var tag in item.tags) {
tag = item.tags[tag];
html.push('<li>' + tag.name + '</li>');
}
html.push('</ul></li>');
html.push('</ul>');
}
$json = json_decode($inputJson, true);
foreach($json as $key => $value)
{
// do somethig
}
Use json_decode
$json = json_decode($some_json, true);
$element1 = $json["item"]["element1"];
$element2 = $json["item"]["element2"];
Repeat to extract all the values you require.

Categories