PHP Json解码代码不工作dailymotion

时间:2015-06-06 08:03:39

标签: php json api

我正在使用php来解码相关视频的dailymotion api,但是我的代码无效。如果我用任何其他单词替换“list”它完美地工作..但我必须使用单词列表,因为dailymotion api ..谁可以提供帮助?

$string='{"list":[
{
    "duration": 233,
    "id": "x2laq9v",
    "thumbnail_480_url": "http://s1.dmcdn.net/JsC-U/x480-8Xl.jpg",
    "title": "Heart Touch Mashup 2015 | Hindi latest Sad Songs | Very Sad Song"

},
{
    "duration": 239,
    "id": "x2laq9v",
    "thumbnail_480_url": "http://s1.dmcdn.net/JsC-U/x480-8Xl.jpg",
    "title": "title 2"
}
]}';

$json_a=json_decode($string,true);

 // array method
foreach($json_a[list] as $p)
{
echo '
     duration: '.$p[duration].'
     <br>title: '.$p[title].'
<br><br>';

}

2 个答案:

答案 0 :(得分:1)

您已将JSON字符串解码为关联数组。您需要通过将键作为字符串来访问元素。否则,PHP会将其视为常量。

所以,改变

SELECT  FullName ,
        LastMonthAmount ,
        ThisMonthAmount ,
        ThisMonthCount
FROM    ( SELECT    SUM([Sales].saleAmount) AS LastMonthAmount ,
                    Sales.cID
          FROM      dbo.[Sales]
                    JOIN Consultants ON Consultants.conID = Sales.cID
          WHERE     YEAR(Sales.salesDate) = YEAR(GETDATE())
                    AND MONTH(Sales.salesDate) = MONTH(GETDATE()) - 1
          GROUP BY  Sales.cID ,
                    Consultants.cName
        ) AS LastMonthAmount
        INNER JOIN ( SELECT SUM([Sales].saleAmount) AS ThisMonthAmount ,
                            Sales.cID ,
                            COUNT(Sales.salesID) AS ThisMonthCount ,
                            Consultants.cName AS FullName
                     FROM   dbo.[Sales]
                            JOIN Consultants ON Consultants.conID = Sales.cID
                     WHERE  YEAR(Sales.salesDate) = YEAR(GETDATE())
                            AND MONTH(Sales.salesDate) = MONTH(GETDATE())
                     GROUP BY Sales.cID ,
                            Consultants.cName
                   ) AS CurrentMonth ON LastMonthAmount.cID = CurrentMonth.cID;

foreach($json_a[list] as $p)
{
    echo '
    duration: '.$p[duration].'
    <br>title: '.$p[title].'
    <br><br>';
}

它应该可以正常工作。

答案 1 :(得分:0)

尝试在数组上加上引号:

foreach($json_a['list'] as $p)