无法使用PHP解析JSON数据

时间:2016-06-08 17:36:58

标签: php json

我正在尝试为我正在构建的应用程序解析一些JSON数据,但我似乎无法让它工作。我之前使用过这段代码,但它不适用于此API。这是我的代码。

{
   "meta":{
      "status":200
   },
   "response":{
      "song":{
         "annotation_count":24,
         "api_path":"/songs/222926",
         "description":{
            "dom":{
               "tag":"root",
               "children":[
                  {
                     "tag":"p",
                     "children":[
                        "As the debut UK single from ",
                        {
                           "tag":"a",
                           "attributes":{
                              "href":"http://genius.com/artists/The-vamps"
                           },
                           "data":{
                              "api_path":"/artists/72916"
                           },
                           "children":[
                              "The Vamps"
                           ]
                        },
                        ", “Can We Dance?” introduces audiences to the group’s fun, catchy sound. The song was featured on the band’s debut album, appropriately named ",
                        {
                           "tag":"a",
                           "attributes":{
                              "href":"http://genius.com/albums/the-vamps/meet-the-vamps"
                           },
                           "data":{
                              "api_path":"/albums/94381"
                           },
                           "children":[
                              {
                                 "tag":"em",
                                 "children":[
                                    "Meet The Vamps"
                                 ]
                              },
                              ","
                           ]
                        },
                        " and was released September 29th, 2013 in the boys' native United Kingdom."
                     ]
                  },
                  "",
                  {
                     "tag":"p",
                     "children":[
                        "In the song, Brad, James, Connor, and Tristan find themselves at a party and are drawn to one particular girl. They’re desperate for their crush to notice them, and use the party as an excuse to flirt, being that they’re drunk and could easily ask her to dance."
                     ]
                  }
               ]
            }
         },
         "embed_content":"<div id='rg_embed_link_222926' class='rg_embed_link' data-song-id='222926'>Read <a href='http://genius.com/The-vamps-can-we-dance-lyrics'>“Can We Dance?” by The Vamps</a> on Genius</div> <script crossorigin src='//genius.com/songs/222926/embed.js'></script>",
         "featured_video":false,
         "full_title":"Can We Dance? by The Vamps",
         "header_image_thumbnail_url":"https://images.genius.com/b3a99070078ce4c6d98fc3dc5ebdacb9.300x300x1.jpg",
         "header_image_url":"https://images.genius.com/b3a99070078ce4c6d98fc3dc5ebdacb9.618x617x1.jpg",
         "id":222926,
         "media":[
            {
               "type":"audio",
               "provider":"spotify",
               "url":"https://open.spotify.com/track/5plDUEEu4r5YEzHL8IhOs1",
               "native_uri":"spotify:track:5plDUEEu4r5YEzHL8IhOs1"
            },
            {
               "type":"video",
               "provider":"youtube",
               "url":"http://www.youtube.com/watch?v=UqSww10eeKw",
               "start":8
            }
         ],
         "path":"/The-vamps-can-we-dance-lyrics",
         "pyongs_count":18,
         "recording_location":null,
         "release_date":null,
         "song_art_image_url":"https://images.genius.com/b3a99070078ce4c6d98fc3dc5ebdacb9.618x617x1.jpg",
         "stats":{
            "accepted_annotations":1,
            "contributors":13,
            "hot":false,
            "iq_earners":13,
            "pageviews":10990,
            "transcribers":1,
            "unreviewed_annotations":22,
            "verified_annotations":0
         },
         "title":"Can We Dance?",
         "url":"http://genius.com/The-vamps-can-we-dance-lyrics",
         "current_user_metadata":{
            "interactions":{
               "pyong":false,
               "following":false
            },
            "relationships":{

            },
            "permissions":[
               "see_pageviews",
               "create_comment"
            ]
         },
         "album":{
            "api_path":"/albums/94381",
            "cover_art_url":"https://images.genius.com/b3a99070078ce4c6d98fc3dc5ebdacb9.618x617x1.jpg",
            "id":94381,
            "name":"Meet The  Vamps",
            "url":"http://genius.com/albums/The-vamps/Meet-the-vamps",
            "artist":{
               "api_path":"/artists/72916",
               "header_image_url":"https://images.genius.com/4575ab8cdc593ab6d4427716b28bda13.525x350x1.jpg",
               "id":72916,
               "image_url":"https://images.genius.com/4575ab8cdc593ab6d4427716b28bda13.525x350x1.jpg",
               "is_verified":false,
               "name":"The Vamps",
               "url":"http://genius.com/artists/The-vamps"
            }
         },
         "custom_performances":[

         ],
         "description_annotation":{
            "_type":"referent",
            "annotator_id":1620931,
            "annotator_login":"BTMN_Gaming",
            "api_path":"/referents/3664740",
            "classification":"accepted",
            "fragment":"Can We Dance?",
            "id":3664740,
            "is_description":true,
            "path":"/3664740/The-vamps-can-we-dance/Can-we-dance",
            "range":{
               "content":"Can We Dance?"
            },
            "song_id":222926,
            "url":"http://genius.com/3664740/The-vamps-can-we-dance/Can-we-dance",
            "verified_annotator_ids":[

            ],
            "annotatable":{
               "api_path":"/songs/222926",
               "client_timestamps":{
                  "updated_by_human_at":1464755813,
                  "lyrics_updated_at":1464755813
               },
               "context":"The Vamps",
               "id":222926,
               "image_url":"https://images.genius.com/b3a99070078ce4c6d98fc3dc5ebdacb9.618x617x1.jpg",
               "link_title":"Can We Dance? by The Vamps",
               "title":"Can We Dance?",
               "type":"Song",
               "url":"http://genius.com/The-vamps-can-we-dance-lyrics"
            },
            "annotations":[
               {
                  "api_path":"/annotations/3664740",
                  "body":{
                     "dom":{
                        "tag":"root",
                        "children":[
                           {
                              "tag":"p",
                              "children":[
                                 "As the debut UK single from ",
                                 {
                                    "tag":"a",
                                    "attributes":{
                                       "href":"http://genius.com/artists/The-vamps"
                                    },
                                    "data":{
                                       "api_path":"/artists/72916"
                                    },
                                    "children":[
                                       "The Vamps"
                                    ]
                                 },
                                 ", “Can We Dance?” introduces audiences to the group’s fun, catchy sound. The song was featured on the band’s debut album, appropriately named ",
                                 {
                                    "tag":"a",
                                    "attributes":{
                                       "href":"http://genius.com/albums/the-vamps/meet-the-vamps"
                                    },
                                    "data":{
                                       "api_path":"/albums/94381"
                                    },
                                    "children":[
                                       {
                                          "tag":"em",
                                          "children":[
                                             "Meet The Vamps"
                                          ]
                                       },
                                       ","
                                    ]
                                 },
                                 " and was released September 29th, 2013 in the boys' native United Kingdom."
                              ]
                           },
                           "",
                           {
                              "tag":"p",
                              "children":[
                                 "In the song, Brad, James, Connor, and Tristan find themselves at a party and are drawn to one particular girl. They’re desperate for their crush to notice them, and use the party as an excuse to flirt, being that they’re drunk and could easily ask her to dance."
                              ]
                           }
                        ]
                     }
                  },
                  "comment_count":0,
                  "community":true,
                  "id":3664740,
                  "pinned":false,
                  "share_url":"http://genius.com/3664740",
                  "state":"accepted",
                  "url":"http://genius.com/3664740/The-vamps-can-we-dance/Can-we-dance",
                  "verified":false,
                  "verified?":false,
                  "votes_total":2,
                  "current_user_metadata":{
                     "interactions":{
                        "vote":null,
                        "cosign":false,
                        "pyong":false
                     },
                     "permissions":[
                        "create_comment"
                     ]
                  },
                  "authors":[
                     {
                        "attribution":0.9484536082474228,
                        "pinned_role":null,
                        "user":{
                           "api_path":"/users/1873201",
                           "avatar":{
                              "tiny":{
                                 "url":"https://images.rapgenius.com/avatars/tiny/35d9463bfb54e2d3d9fb261fc6ef2174",
                                 "bounding_box":{
                                    "width":16,
                                    "height":16
                                 }
                              },
                              "thumb":{
                                 "url":"https://images.rapgenius.com/avatars/thumb/35d9463bfb54e2d3d9fb261fc6ef2174",
                                 "bounding_box":{
                                    "width":32,
                                    "height":32
                                 }
                              },
                              "small":{
                                 "url":"https://images.rapgenius.com/avatars/small/35d9463bfb54e2d3d9fb261fc6ef2174",
                                 "bounding_box":{
                                    "width":100,
                                    "height":100
                                 }
                              },
                              "medium":{
                                 "url":"https://images.rapgenius.com/avatars/medium/35d9463bfb54e2d3d9fb261fc6ef2174",
                                 "bounding_box":{
                                    "width":300,
                                    "height":400
                                 }
                              }
                           },
                           "header_image_url":"https://images.rapgenius.com/avatars/medium/35d9463bfb54e2d3d9fb261fc6ef2174",
                           "human_readable_role_for_display":"Editor",
                           "id":1873201,
                           "iq":18561,
                           "login":"VinylKilljoy_MCR",
                           "name":"VinylZombie_MCR",
                           "role_for_display":"editor",
                           "url":"http://genius.com/VinylKilljoy_MCR"
                        }
                     },
                     {
                        "attribution":0.051546391752577324,
                        "pinned_role":null,
                        "user":{
                           "api_path":"/users/1620931",
                           "avatar":{
                              "tiny":{
                                 "url":"https://assets.genius.com/images/default_avatar_16.png?1465401450",
                                 "bounding_box":{
                                    "width":16,
                                    "height":16
                                 }
                              },
                              "thumb":{
                                 "url":"https://assets.genius.com/images/default_avatar_32.png?1465401450",
                                 "bounding_box":{
                                    "width":32,
                                    "height":32
                                 }
                              },
                              "small":{
                                 "url":"https://assets.genius.com/images/default_avatar_100.png?1465401450",
                                 "bounding_box":{
                                    "width":100,
                                    "height":100
                                 }
                              },
                              "medium":{
                                 "url":"https://assets.genius.com/images/default_avatar_300.png?1465401450",
                                 "bounding_box":{
                                    "width":300,
                                    "height":400
                                 }
                              }
                           },
                           "header_image_url":"https://assets.genius.com/images/default_avatar_300.png?1465401450",
                           "human_readable_role_for_display":null,
                           "id":1620931,
                           "iq":38,
                           "login":"BTMN_Gaming",
                           "name":"BTMN_Gaming",
                           "role_for_display":null,
                           "url":"http://genius.com/BTMN_Gaming"
                        }
                     }
                  ],
                  "cosigned_by":[

                  ],
                  "verified_by":null
               }
            ]
         },
         "featured_artists":[

         ],
         "primary_artist":{
            "api_path":"/artists/72916",
            "header_image_url":"https://images.genius.com/4575ab8cdc593ab6d4427716b28bda13.525x350x1.jpg",
            "id":72916,
            "image_url":"https://images.genius.com/4575ab8cdc593ab6d4427716b28bda13.525x350x1.jpg",
            "is_verified":false,
            "name":"The Vamps",
            "url":"http://genius.com/artists/The-vamps"
         },
         "producer_artists":[

         ],
         "song_relationships":[
            {
               "type":"samples",
               "songs":[

               ]
            },
            {
               "type":"sampled_in",
               "songs":[

               ]
            },
            {
               "type":"interpolates",
               "songs":[

               ]
            },
            {
               "type":"interpolated_by",
               "songs":[

               ]
            },
            {
               "type":"cover_of",
               "songs":[

               ]
            },
            {
               "type":"covered_by",
               "songs":[

               ]
            },
            {
               "type":"remix_of",
               "songs":[

               ]
            },
            {
               "type":"remixed_by",
               "songs":[

               ]
            },
            {
               "type":"live_version_of",
               "songs":[

               ]
            },
            {
               "type":"performed_live_as",
               "songs":[

               ]
            }
         ],
         "verified_annotations_by":[

         ],
         "verified_lyrics_by":[

         ],
         "writer_artists":[

         ]
      }
   }
}

我在问题的最后引用了JSON。它根据http://jsonlint.com有效。

我也尝试过使用这段代码,但没有运气。

do.call(rbind, lapply())

我确定它很小,但我无法理解。

这是JSON:

df1 <- data.frame(x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10))
df2 <- data.frame(x1 = rnorm(10, 5), x2 = rnorm(10), x3 = rnorm(10))

getp <- function(var) {
  return(t.test(df1[, var], df2[, var])$p.value)
}

list <- c('x1', 'x2', 'x3')
ps <- do.call(rbind, lapply(list, getp))
ps
                 [,1]
[1,] 6.232025e-09
[2,] 2.128019e-09
[3,] 5.824713e-08

2 个答案:

答案 0 :(得分:1)

您可以先尝试循环访问数据,因为数据结构比使用$ songs [0]访问要复杂得多。试试这个:

    <?php

        $link           = "http://api.genius.com/songs/542383?access_token=HOlDPArpVrb8ljFKUN6FyBjSG--hb0QHv9qFEccs2gNdCr62W0pJuikv1RoCASgY";
        $json           = file_get_contents($link);
        $obj            = json_decode($json);
        $arrChildren    = $obj->response->song->description->dom->children;

        foreach($arrChildren as $strKey=>$objData){

            if (isset($objData->children) && @is_string($objData->children)) {
                echo $objData->children . "<br />";
            }else if (isset($objData->children) && is_array($objData->children)) {
                foreach ($objData->children as $iKey => $objNested) {
                    if (is_string($objNested)) {
                        echo $objNested . "<br />";
                    }
                    else {
                        if (is_object($objNested)) {
                            var_dump($objNested);
                            echo "<br /><br />";
                        }
                    }
                }
            }

        }

        var_dump($arrChildren);

<强>输出:

        Taylor Swift unveiled a little snippet of this song in her new Diet Coke ad.

        object(stdClass)[7]
          public 'tag' => string 'a' (length=1)
          public 'attributes' =>
            object(stdClass)[8]
              public 'href' => string 'https://twitter.com/taylorswift13/status/522413036729298944' (length=59)
              public 'rel' => string 'nofollow' (length=8)
          public 'children' =>
            array (size=1)
              0 => string 'https://twitter.com/taylorswift13/status/522413036729298944' (length=59)


        The song works as an advice to a guy who wants to get a girl back. She suggests better tactics than the ones her former lovers seemed to have used on her, described in songs like

            object(stdClass)[10]
              public 'tag' => string 'a' (length=1)
              public 'attributes' =>
                object(stdClass)[11]
                  public 'href' => string 'http://genius.com/Taylor-swift-we-are-never-ever-getting-back-together-lyrics' (length=77)
              public 'data' =>
                object(stdClass)[12]
                  public 'api_path' => string '/songs/84880' (length=12)
              public 'children' =>
                array (size=1)
                  0 => string '“We Are Never Ever Getting Back Together”' (length=45)


                        .
            array (size=5)
              0 =>
                object(stdClass)[5]
                  public 'tag' => string 'p' (length=1)
                  public 'children' =>
                    array (size=1)
                      0 => string 'Taylor Swift unveiled a little snippet of this song in her new Diet Coke ad.' (length=76)
              1 => string '' (length=0)
              2 =>
                object(stdClass)[6]
                  public 'tag' => string 'p' (length=1)
                  public 'children' =>
                    array (size=1)
                      0 =>
                        object(stdClass)[7]
                          ...
              3 => string '' (length=0)
              4 =>
                object(stdClass)[9]
                  public 'tag' => string 'p' (length=1)
                  public 'children' =>
                    array (size=3)
                      0 => string 'The song works as an advice to a guy who wants to get a girl back. She suggests better tactics than the ones her former lovers seemed to have used on her, described in songs like ' (length=179)
                      1 =>
                        object(stdClass)[10]
                          ...
                      2 => string '.' (length=1)

答案 1 :(得分:0)

使用时检查第4行:

echo $row['description']['children']; 

一定是

echo $item['description']['children'];