Quick JSON/PHP question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BryanA

    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?
  • Erwin Moller

    #2
    Re: Quick JSON/PHP question

    BryanA schreef:

    Hi Bryan,
    Is it possible to parse JSON data
    yes:
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    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
    --

    Comment

    • Jerry Stuckle

      #3
      Re: Quick JSON/PHP question

      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.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • BryanA

        #4
        Re: Quick JSON/PHP question

        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

        Comment

        • Jerry Stuckle

          #5
          Re: Quick JSON/PHP question

          BryanA wrote:
          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
          =============== ===

          Comment

          • BryanA

            #6
            Re: Quick JSON/PHP question

            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($js on);
            foreach($obj->{'by'} as $val){
            echo $val;
            }

            Any help is greatly appreciated

            Comment

            • BryanA

              #7
              Re: Quick JSON/PHP question

              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?

              Comment

              • Jerry Stuckle

                #8
                Re: Quick JSON/PHP question

                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.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                • BryanA

                  #9
                  Re: Quick JSON/PHP question

                  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...@attgl obal.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

                  Comment

                  • Norman Peelman

                    #10
                    Re: Quick JSON/PHP question

                    BryanA wrote:
                    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

                    Working...