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>';
}
Related
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 4 years ago.
im working on a project but I ran into a problem :(
I looked on the internet for like 2-3 hours and cannot find the way how to do it...
I retrieve this JSON from my database:
{
"articles": {
"1": {"discription":"Transport", "tax":"21", "price":"234"
},
"2": {"discription":"Opslag", "tax":"19", "price":"19"}
}
}
What I want to do next is retrieve the description,tax & Price from each object, how to do this with a for each loop on the most efficient way?
Thanks!
You can use following code snippet to get value of mentioned keys-
<?php
$str = '{
"articles": {
"1": {"discription":"Transport", "tax":"21", "price":"234"
},
"2": {"discription":"Opslag", "tax":"19", "price":"19"}
}
}';
//convert json to array
$jsonToArray = json_decode($str, true);
foreach ($jsonToArray['articles'] as $value) {
echo $value['discription'] . ' ' . $value['tax'] . ' ' . $value['price'] . PHP_EOL;
}
if you want to access those keys with object then you can proceed with following code snippet-
<?php
$str = '{
"articles": {
"1": {"discription":"Transport", "tax":"21", "price":"234"
},
"2": {"discription":"Opslag", "tax":"19", "price":"19"}
}
}';
//convert json to object
$jsonToObject = json_decode($str);
foreach ($jsonToObject->articles as $value) {
echo $value->discription . ' ' . $value->tax . ' ' . $value->price . PHP_EOL;
}
You are free to make changes in the echo statement that suits your requirement.
Checkout my all the technical post.
$array = json_decode($json_string);
for($i = 0; $i<sizeof($array); $i++) {
echo $array[$i]['description'];
echo $array[$i]['tax'];
echo $array[$i]['price'];
}
You are looking for the json_decode function before you do that, here's an example:
$jsonDecoded = json_decode($jsonInput);
foreach ($jsonDecoded->articles as $item) {
echo $item->discription . "<br>";
echo $item->tax . "<br>";
echo $item->price . "<br>";
}
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>';
I'm new to PHP and try to echo out some data from json, but I got stucked in this.
It shows non of the data, but the data is there. The var_dump() shows it to me.
Probably I don't use the array correctly, but I can't find what's wrong. I've this code,
I request some data which I gathered with knockout.js
Knockout gives me a json (as show below)
$json = $_REQUEST[seats];
echo 'requested data raw '. "<br>".$json;
$data = json_decode($json, true);
echo "<br>".'var_dump '. "<br>";
var_dump($data);
foreach ($data as $optie ) {
echo "name = " . $optie->name . "<br>";
echo "optie = " . $optie->optieName . "<br>";
echo "prijs = " . $optie->prijs . "<br>";
}
This is my JSON:
[
{
"name": "Naam 1",
"optie": {
"optieName": "Make_up",
"prijs": 9.95
},
"PrijsFormated": "Euro: 9.95"
},
{
"name": "Naam 2",
"optie": {
"optieName": "Handverzorging",
"prijs": 12.95
},
"PrijsFormated": "Euro: 12.95"
}
]
You should use such loop:
foreach ($data as $optie ) {
echo "name = " . $optie['name'] . "<br>";
echo "optie = " . $optie['optie']['optieName'] . "<br>";
echo "prijs = " . $optie['optie']['prijs']. "<br>";
}
Because using json_decode() with second parameter as true you have created associative array - documentation.
If you would like to access data as object, you should use:
$data = json_decode($json);
instead of
$data = json_decode($json, true);
and then you should use the following loop:
foreach ($data as $optie ) {
echo "name = " . $optie->name . "<br>";
echo "optie = " . $optie->optie->optieName . "<br>";
echo "prijs = " . $optie->optie->prijs. "<br>";
}
Why i can't get out the values from the json file with PHP?
I get zero values? Have tried for hours.
<?php
$json_string = 'http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=44';
$jsondata = file_get_contents($json_string);
$data = json_decode($jsondata, TRUE);
print_r($data);
echo "<br><br><br><br>";
foreach ($data as $recenttrades) {
echo "VALUES('{$recenttrades->quantity}', '{$recenttrades->price}' ";
}
?>
Update: but can't get the value from primaryname and primarycode.
I have tried this:
$json_string = 'http://pubapi.cryptsy.com/api.php?method=marketdatav2';
$jsondata = file_get_contents($json_string);
$data = json_decode($jsondata, TRUE);
//print_r($data);
foreach ($data["market"] as $markets) {
echo "Primary code: <strong>{$markets['primarycode']}</strong><br>";
foreach($markets as $market) {
foreach($market as $attributes) {
foreach($attributes["recenttrades"] as $recenttrade) {
echo "quantity: " . $recenttrade['quantity'] .", price: " . $recenttrade['price'] . "<br>";
}
}
}
}
Others have mentioned that you're dealing with nested arrays, not objects. Along with pointing out the issue of being nested rather deeply, I would suggest digging down with foreach (I will probably be crucified for this):
<?php
$json_string = 'http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=44';
$jsondata = file_get_contents($json_string);
$data = json_decode($jsondata, TRUE);
//print_r($data);
echo "<br><br><br><br>";
foreach ($data as $markets) {
foreach($markets as $market) {
foreach($market as $attributes) {
foreach($attributes["recenttrades"] as $recenttrade) {
//echo "<pre>";
//print_r($recenttrade);
//echo "</pre>";
echo "VALUES('{quantity: " . $recenttrade['quantity'] ."}', 'price: {" . $recenttrade['price'] . "}')";
}
}
}
}
?>
This will ensure that you grab every recentrades item at this level of the array. This way you are prepared for other markets to be added to the API and your code isn't locked into searching only in the item named "FST".
recenttrades is nested pretty deeply in that array. Try
foreach ($data['return']['markets']['FST']['recenttrades'] as $recenttrades) {
recenttrades is several levels nested, so simply doing foreach($data as $recenttrades) is not sufficient.
You need to do:
$recentTrades = $data['return']['markets']['FST']['recenttrades'];
foreach($recentTrades as $recentTrade) {
...
}
recenttrades is nested deeply and you're asking for arrays, not objects. This seems to work:
foreach ($data['return']['markets']['FST']['recenttrades'] as $recenttrades) {
echo "VALUES('{$recenttrades['quantity']}', '{$recenttrades['price']}' ";
}
In response to your update, where you want to loop over markets, try this:
foreach ($data['return']['markets'] as $market) {
echo "Primary code: <strong>{$market['primarycode']}</strong><br>";
foreach ($market["recenttrades"] as $recenttrade) {
echo "quantity: " . $recenttrade['quantity'] .", price: " . $recenttrade['price'] . "<br>";
}
}
If I wanted to echo the link of a general thread on 4chan.
Here's what I was thinking of, however I have no idea on what to do
$jsonurl = "http://a.4cdn.org/vg/catalog.json";
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
foreach( $json_output as $no )
{
if(strpos(sub, 'DOTA')) { //Not sure how I would do this
//echo the "no" of it in the json
}
}
Had to look at the JSON data to see what you were looking for. Give this a try
$jsonurl = "http://a.4cdn.org/g/catalog.json";
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
foreach ($json_output as $page) {
foreach($page->threads as $thread) {
if (isset($thread->sub)) {
$sub = $thread->sub;
$no = $thread->no;
echo $sub . ', Thread Number: ' . $no . '<br />';
/*
if (strpos($sub, 'DOTA') !== false) {
echo 'Found DOTA!!! Thread Number is: ' . $thread->no;
}
*/
}
}
}
You should study the JSON data that you receive in order to know how to extract the data. This is one way of doing it:
foreach( $json_output as $page ) {
foreach ($page->threads as $t) {
echo 'http://boards.4chan.org/vg/res/' . $t->no . ', ';
}
}