JSON and PHP Coding Examples

We will try and answer Questions in this forum. If you are having any issues with iScore Baseball, this is probably the best place to start. You can also search historical posts here.
Post Reply
User avatar
Egghead_#8
Posts: 72
Joined: Fri Jul 09, 2010 6:51 am

JSON and PHP Coding Examples

Post by Egghead_#8 » Mon Apr 29, 2013 10:08 am

Does anyone have examples of PHP code they've used to parse the JSON data that comes from the iScore Team Website API? Before reinventing the wheel, I thought it'd be great if someone could share sample code they've written. The examples you find online with a general Google search are simplistic and the amount / complexity of the API's JSON nodes don't compare to what the API provides. thanks
User avatar
elcray
Posts: 97
Joined: Thu Apr 26, 2012 5:35 am
Location: Sedalia, MO

Re: JSON and PHP Coding Examples

Post by elcray » Tue Apr 30, 2013 4:48 am

I code in vb.net so I'm not sure this will help you. But below is one of my parsing subs:

Code: Select all

Private Sub getParseXMLGameDetail()
        Dim XMLFile As String = strXMLFile
        Dim xmlDoc As New XmlDocument

        Dim iEventID As Integer

        Dim strType As String
        Dim strDesc As String
        Dim strX As String
        Dim strY As String
        Dim strHit_Type As String
        Dim strHit_Strength As String
        Dim strPlayer As String
        Dim strBase As String
        Dim strFielders As String

        Dim strTeamV As String
        Dim strTeamH As String
        Dim strTeamFielding As String
        Dim strSide As String


        Dim str1v As String = ""
        Dim str2v As String = ""
        Dim str3v As String = ""
        Dim str4v As String = ""
        Dim str5v As String = ""
        Dim str6v As String = ""
        Dim str7v As String = ""
        Dim str8v As String = ""
        Dim str9v As String = ""

        Dim str1h As String = ""
        Dim str2h As String = ""
        Dim str3h As String = ""
        Dim str4h As String = ""
        Dim str5h As String = ""
        Dim str6h As String = ""
        Dim str7h As String = ""
        Dim str8h As String = ""
        Dim str9h As String = ""

        xmlDoc.Load(XMLFile)
        Dim node As XmlNode = xmlDoc.SelectSingleNode("/GAME")

        If node IsNot Nothing Then
            setInsertGame(node.Attributes("guid").Value, node.Attributes("name").Value)
            For Each RootLevel As XmlNode In node.ChildNodes
                Select Case RootLevel.Name
                    Case Is = "TEAM"

                        Select Case RootLevel.Attributes("side").Value
                            Case Is = "home"
                                strTeamH = RootLevel.Attributes("guid").Value
                            Case Is = "visitor"
                                strTeamV = RootLevel.Attributes("guid").Value
                        End Select

                        setInsertTeam(node.Attributes("guid").Value, RootLevel.Attributes("guid").Value, RootLevel.Attributes("side").Value, RootLevel.Attributes("name").Value)
                        For Each TEAMLevel As XmlNode In RootLevel.ChildNodes
                            Select Case TEAMLevel.Name
                                Case Is = "PLAYER"

                                    setInsertPlayer(node.Attributes("guid").Value, _
                                                    RootLevel.Attributes("guid").Value, _
                                                    TEAMLevel.Attributes("guid").Value, _
                                                    TEAMLevel.Attributes("firstname").Value, _
                                                    TEAMLevel.Attributes("lastname").Value, _
                                                    TEAMLevel.Attributes("bats").Value, _
                                                    TEAMLevel.Attributes("throws").Value, _
                                                    TEAMLevel.Attributes("jersey").Value)


                            End Select
                        Next
                    Case Is = "INNING"
                        If RootLevel.Attributes("tb").Value = "T" Then
                            strTeamFielding = strTeamH
                        Else
                            strTeamFielding = strTeamV
                        End If
                        strSide = RootLevel.Attributes("tb").Value

                        For Each INNINGLevel As XmlNode In RootLevel.ChildNodes
                            For Each BATTERLevel As XmlNode In INNINGLevel.ChildNodes
                                Select Case BATTERLevel.Name
                                    Case Is = "PITCH"
                                        
                                        setInsertPitch(node.Attributes("guid").Value, _
                                                       BATTERLevel.Attributes("id").Value, _
                                                       RootLevel.Attributes("num").Value, _
                                                       RootLevel.Attributes("tb").Value, _
                                                       BATTERLevel.Attributes("time").Value, _
                                                       BATTERLevel.Attributes("ms").Value, _
                                                       BATTERLevel.Attributes("pitcher").Value, _
                                                       BATTERLevel.Attributes("type").Value, _
                                                       BATTERLevel.Attributes("speed").Value, _
                                                       BATTERLevel.Attributes("batterhand").Value, _
                                                       BATTERLevel.Attributes("pitcherhand").Value, _
                                                       BATTERLevel.Attributes("x").Value, _
                                                       BATTERLevel.Attributes("y").Value, _
                                                       INNINGLevel.Attributes("guid").Value)

                                        iEventID = 0
                                        For Each PITCHLevel As XmlNode In BATTERLevel.ChildNodes

                                            Select Case PITCHLevel.Name
                                                Case Is = "LINEUP"
                                                    For Each LINEUPLevel As XmlNode In PITCHLevel.ChildNodes
                                                        Select Case LINEUPLevel.Name
                                                            Case Is = "POS"
                                                                If PITCHLevel.Attributes("tguid").Value = strTeamH Then
                                                                    Select Case LINEUPLevel.Attributes("field").Value
                                                                        Case Is = "1"
                                                                            str1h = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "2"
                                                                            str2h = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "3"
                                                                            str3h = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "4"
                                                                            str4h = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "5"
                                                                            str5h = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "6"
                                                                            str6h = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "7"
                                                                            str7h = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "8"
                                                                            str8h = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "9"
                                                                            str9h = LINEUPLevel.Attributes("guid").Value
                                                                    End Select
                                                                Else
                                                                    Select Case LINEUPLevel.Attributes("field").Value
                                                                        Case Is = "1"
                                                                            str1v = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "2"
                                                                            str2v = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "3"
                                                                            str3v = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "4"
                                                                            str4v = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "5"
                                                                            str5v = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "6"
                                                                            str6v = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "7"
                                                                            str7v = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "8"
                                                                            str8v = LINEUPLevel.Attributes("guid").Value
                                                                        Case Is = "9"
                                                                            str9v = LINEUPLevel.Attributes("guid").Value
                                                                    End Select
                                                                End If

                                                                setInsertPOS(node.Attributes("guid").Value, _
                                                                        BATTERLevel.Attributes("id").Value, _
                                                                        PITCHLevel.Attributes("tguid").Value, _
                                                                        LINEUPLevel.Attributes("guid").Value, _
                                                                        LINEUPLevel.Attributes("bat").Value, _
                                                                        LINEUPLevel.Attributes("field").Value)

                                                        End Select
                                                    Next
                                                Case Is = "SITUATION"
                                                    
                                                    setInsertSituation(node.Attributes("guid").Value, _
                                                       BATTERLevel.Attributes("id").Value, _
                                                       PITCHLevel.Attributes("visitor").Value, _
                                                       PITCHLevel.Attributes("home").Value, _
                                                       PITCHLevel.Attributes("balls").Value, _
                                                       PITCHLevel.Attributes("strikes").Value, _
                                                       PITCHLevel.Attributes("outs").Value, _
                                                       PITCHLevel.Attributes("first").Value, _
                                                       PITCHLevel.Attributes("second").Value, _
                                                       PITCHLevel.Attributes("third").Value, _
                                                       PITCHLevel.Attributes("pitcherballs").Value, _
                                                       PITCHLevel.Attributes("pitcherstrikes").Value)


                                                Case Is = "EVENT"
                                                    iEventID += 1
                                                    strType = ""
                                                    strDesc = ""
                                                    strX = "0"
                                                    strY = "0"
                                                    strHit_Type = ""
                                                    strHit_Strength = ""
                                                    strPlayer = ""
                                                    strBase = ""
                                                    strFielders = ""

                                                    For Each EVENTAtt As XmlAttribute In PITCHLevel.Attributes
                                                        Select Case EVENTAtt.Name
                                                            Case Is = "type"
                                                                strType = EVENTAtt.Value
                                                            Case Is = "desc"
                                                                strDesc = EVENTAtt.Value
                                                            Case Is = "x"
                                                                strX = EVENTAtt.Value
                                                            Case Is = "y"
                                                                strY = EVENTAtt.Value
                                                            Case Is = "hit_type"
                                                                strHit_Type = EVENTAtt.Value
                                                            Case Is = "hit_strength"
                                                                strHit_Strength = EVENTAtt.Value
                                                            Case Is = "player"
                                                                strPlayer = EVENTAtt.Value
                                                            Case Is = "base"
                                                                strBase = EVENTAtt.Value
                                                            Case Is = "fielders"
                                                                strFielders = EVENTAtt.Value
                                                        End Select
                                                    Next

                                                    setInsertEvent(node.Attributes("guid").Value, _
                                                                                  BATTERLevel.Attributes("id").Value, _
                                                                                  iEventID, _
                                                                                  strType, _
                                                                                  strDesc, _
                                                                                  strX, _
                                                                                  strY, _
                                                                                  strHit_Type, _
                                                                                  strHit_Strength, _
                                                                                  strPlayer, _
                                                                                  strBase, _
                                                                                  strFielders)





                                                Case Is = "RESULT"
                                                    setInsertRESULT(node.Attributes("guid").Value, _
                                                                BATTERLevel.Attributes("id").Value, _
                                                                (PITCHLevel.Attributes("visitor").Value), _
                                                                (PITCHLevel.Attributes("home").Value), _
                                                                (PITCHLevel.Attributes("balls").Value), _
                                                                (PITCHLevel.Attributes("strikes").Value), _
                                                                (PITCHLevel.Attributes("outs").Value), _
                                                                (PITCHLevel.Attributes("first").Value), _
                                                                (PITCHLevel.Attributes("second").Value), _
                                                                (PITCHLevel.Attributes("third").Value), _
                                                                (PITCHLevel.Attributes("pitcherballs").Value), _
                                                                (PITCHLevel.Attributes("pitcherstrikes").Value))

                                            End Select
                                        Next
                                        'insert POSByPitch
                                        Select Case strSide
                                            Case Is = "T"
                                                setInsertPOSByPitch(node.Attributes("guid").Value, _
                                                                BATTERLevel.Attributes("id").Value, _
                                                                str1h, _
                                                                str2h, _
                                                                str3h, _
                                                                str4h, _
                                                                str5h, _
                                                                str6h, _
                                                                str7h, _
                                                                str8h, _
                                                                str9h)
                                            Case Is = "B"
                                                setInsertPOSByPitch(node.Attributes("guid").Value, _
                                                                BATTERLevel.Attributes("id").Value, _
                                                                str1v, _
                                                                str2v, _
                                                                str3v, _
                                                                str4v, _
                                                                str5v, _
                                                                str6v, _
                                                                str7v, _
                                                                str8v, _
                                                                str9v)
                                        End Select


                                End Select
                            Next
                        Next
                    Case Else

                End Select

            Next
        End If
    End Sub
Post Reply