Thanks guys. My problem now is parsing the JSON data.
Here is what it is to start:
{
"songs": [
{
"by": "ARTIST",
"cover": "COVERARTUR L",
"id": 0,
"rank": 0,
"title": "SONGNAME",
"type": "SONG"
},
{
"by": "ARTIST2",
"cover": "COVERARTUR L2",
"id": 1,
"rank": 1,
"title": "SONGNAME2" ,
"type": "song"
}
]
}
I can easily get all the data when there is a single entry by using
JSON decode and then echoing the object by name but when there is more
than one entry it doesn't show anything even when I try to do a
foreach loop. Here is the code that doesn't work on more than one
entry:
Thanks guys. My problem now is parsing the JSON data.
>
Here is what it is to start:
>
{
"songs": [
{
"by": "ARTIST",
"cover": "COVERARTUR L",
"id": 0,
"rank": 0,
"title": "SONGNAME",
"type": "SONG"
},
{
"by": "ARTIST2",
"cover": "COVERARTUR L2",
"id": 1,
"rank": 1,
"title": "SONGNAME2" ,
"type": "song"
}
]
}
I can easily get all the data when there is a single entry by using
JSON decode and then echoing the object by name but when there is more
than one entry it doesn't show anything even when I try to do a
foreach loop. Here is the code that doesn't work on more than one
entry:
>
$json = '{
"songs": [
{
"by": "ARTIST",
"cover": "COVERARTUR L",
"id": 0,
"rank": 0,
"title": "SONGNAME",
"type": "SONG"
},
{
"by": "ARTIST2",
"cover": "COVERARTUR L2",
"id": 1,
"rank": 1,
"title": "SONGNAME2" ,
"type": "song"
}
]
}';
$obj = json_decode($js on);
foreach($obj->{'by'} as $val){
echo $val;
}
>
Any help would be greatly appreciated
-Bryan
>
>
Take out your foreach() loop and put the following in:
print_r($obj);
That should help you see what your problem is.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. jstucklex@attgl obal.net
=============== ===
I can parse single entries but as soon as it has two the page goes
blank. I have tried using a for each loop with JSON decode but it only
works when there is one.
Here is the parser that doesn't work:
Thanks Jerry. Not sure what the deal was with the double post but when
I print the object the result is:
>
stdClass Object ( [songs] =Array ( [0] =stdClass Object ( [by] =>
ARTIST [cover] =COVERARTURL [id] =0 [rank] =0 [title] =>
SONGNAME [type] =SONG ) [1] =stdClass Object ( [by] =ARTIST2
[cover] =COVERARTURL2 [id] =1 [rank] =1 [title] =SONGNAME2
[type] =song ) ) )
>
How can I get the individual values?
>
That's correct. $obj has one member (songs), which is an array of
objects. You would use
foreach($obj->{'songs'} as $song) {...
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. jstucklex@attgl obal.net
=============== ===
>
That's correct. $obj has one member (songs), which is an array of
objects. You would use
>
foreach($obj->{'songs'} as $song) {...
>
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attgl obal.net
=============== ===
I'm not sure I get it. The out put of the print_r($obj) is:
)
So how would I get the value of each one the values of [0] and the
values of [1] into a variable separate respective values? Something
were I could call it by $something[1]->by and it would return ARTIST2
On Oct 17, 6:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>BryanA wrote:
>>Thanks Jerry. Not sure what the deal was with the double post but when
>>I print the object the result is:
>>stdClass Object ( [songs] =Array ( [0] =stdClass Object ( [by] =>
>>ARTIST [cover] =COVERARTURL [id] =0 [rank] =0 [title] =>
>>SONGNAME [type] =SONG ) [1] =stdClass Object ( [by] =ARTIST2
>>[cover] =COVERARTURL2 [id] =1 [rank] =1 [title] =SONGNAME2
>>[type] =song ) ) )
>>How can I get the individual values?
>That's correct. $obj has one member (songs), which is an array of
>objects. You would use
>>
>foreach($obj->{'songs'} as $song) {...
>>
>--
>============== ====
>Remove the "x" from my email address
>Jerry Stuckle
>JDS Computer Training Corp.
>jstuck...@attg lobal.net
>============== ====
>
I'm not sure I get it. The out put of the print_r($obj) is:
>
stdClass Object
(
[songs] =Array
(
[0] =stdClass Object
(
[by] =ARTIST
[cover] =COVERARTURL
[id] =0
[rank] =0
[title] =SONGNAME
[type] =SONG
)
>
[1] =stdClass Object
(
[by] =ARTIST2
[cover] =COVERARTURL2
[id] =1
[rank] =1
[title] =SONGNAME2
[type] =song
)
>
)
>
)
So how would I get the value of each one the values of [0] and the
values of [1] into a variable separate respective values? Something
were I could call it by $something[1]->by and it would return ARTIST2
>
I really appreciate your help
That's what Jerry is telling you:
foreach($obj as $song)
{
echo $song->title.': ';
echo $song->by;
}
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Comment