473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Quick JSON/PHP question

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
9 2201
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post...
4
by: im12345 | last post by:
I have the following question: Im doing a sample application using dojo and json. I have 2 classes: 1. Book class package com.esolaria.dojoex; import org.json.JSONObject; import...
2
by: vunet | last post by:
When implementing JSON as a form of data exchange between server and client, what security measures do I need to consider? For example, I have XMLHttpRequest returning JSON text from the server and...
6
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.