By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,888 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,888 IT Pros & Developers. It's quick & easy.

Quick JSON/PHP question

P: n/a
Is it possible to parse JSON data and then post it to a mysql db with
the data in their respective fields?
Oct 17 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
BryanA schreef:

Hi Bryan,
Is it possible to parse JSON data
yes:
http://nl3.php.net/manual/en/function.json-decode.php

Have a look at example 1.

and then post it to a mysql db with
the data in their respective fields?
I wouldn't say 'post it to a database', but insert it into a database.
And yes, that too is perfectly possible.

Simply use the returned array (from json-decode) in this way:

foreach ($JSonArr as $key=>$val){
// Do Insert using $key and $val
}

However, be aware of SQL-injection. Everybody can construct a JSON and
send it to your script.

Regards,
Erwin Moller
--
Oct 17 '08 #2

P: n/a
BryanA wrote:
Is it possible to parse JSON data and then post it to a mysql db with
the data in their respective fields?
Yes.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 17 '08 #3

P: n/a
Thanks guys. My problem now is parsing the JSON data.

Here is what it is to start:

{
"songs": [
{
"by": "ARTIST",
"cover": "COVERARTURL",
"id": 0,
"rank": 0,
"title": "SONGNAME",
"type": "SONG"
},
{
"by": "ARTIST2",
"cover": "COVERARTURL2",
"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": "COVERARTURL",
"id": 0,
"rank": 0,
"title": "SONGNAME",
"type": "SONG"
},
{
"by": "ARTIST2",
"cover": "COVERARTURL2",
"id": 1,
"rank": 1,
"title": "SONGNAME2",
"type": "song"
}
]
}';
$obj = json_decode($json);
foreach($obj->{'by'} as $val){
echo $val;
}

Any help would be greatly appreciated
-Bryan

Oct 17 '08 #4

P: n/a
BryanA wrote:
Thanks guys. My problem now is parsing the JSON data.

Here is what it is to start:

{
"songs": [
{
"by": "ARTIST",
"cover": "COVERARTURL",
"id": 0,
"rank": 0,
"title": "SONGNAME",
"type": "SONG"
},
{
"by": "ARTIST2",
"cover": "COVERARTURL2",
"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": "COVERARTURL",
"id": 0,
"rank": 0,
"title": "SONGNAME",
"type": "SONG"
},
{
"by": "ARTIST2",
"cover": "COVERARTURL2",
"id": 1,
"rank": 1,
"title": "SONGNAME2",
"type": "song"
}
]
}';
$obj = json_decode($json);
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.
js*******@attglobal.net
==================

Oct 17 '08 #5

P: n/a
Thanks guys. My problem now is parsing multiple JSON strings.
Here's what the JSON is:

{
"songs": [
{
"by": "artist",
"cover": "albumart",
"id": 1,
"rank": 1,
"title": "song name",
"type": "song"
},
{
"by": "artist1",
"cover": "albumart1",
"id": 2,
"rank": 2,
"title": "song name1",
"type": "song"
}
]
}

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:

$json = '{
"songs": [
{
"by": "artist",
"cover": "albumart",
"id": 1,
"rank": 1,
"title": "song name",
"type": "song"
},
{
"by": "artist1",
"cover": "albumart1",
"id": 2,
"rank": 2,
"title": "song name1",
"type": "song"
}
]
}';
$obj = json_decode($json);
foreach($obj->{'by'} as $val){
echo $val;
}

Any help is greatly appreciated
Oct 17 '08 #6

P: n/a
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?
Oct 18 '08 #7

P: n/a
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.
js*******@attglobal.net
==================

Oct 18 '08 #8

P: n/a
On Oct 17, 6:05*pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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
Oct 18 '08 #9

P: n/a
BryanA wrote:
On Oct 17, 6:05 pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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?-
Oct 18 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.