Json从[foreach warning]开始

时间:2013-06-03 11:21:18

标签: php json decode

我需要抓一些json代码;它以[

开头

如果我尝试循环播放,我会检索

  

警告:为foreach()提供的参数无效

有人可以帮我正确解码这个json吗?感谢

PHP代码:

echo "<h2>Tradedoubler Coupon</h2>";
$tdjson = file_get_contents("http://api.tradedoubler.com/1.0/vouchers.json;voucherTypeId=1?token=280C407EC3D32D2C8E4C04A100DD00FEA9000580");
//$tdobj = json_decode(print_r($tdjson));
$tdobj = json_decode($tdjson,true);
//foreach($tdobj as $prog ) {
echo '<ul>';
echo '<li>' . print_r($tdobj) . '</li>
</ul>';

JSON文件:

[
    {
        id: 20108,
        programId: 17269,
        programName: "",
        code: "6G4KGGDH",
        updateDate: "1369813973159",
        publishStartDate: "1370037600000",
        publishEndDate: "1372629599999",
        startDate: "1370037600000",
        endDate: "1372629599999",
        title: "Sconto",
        shortDescription: "kkkkk",
        description: "kkkk",
        voucherTypeId: 1,
        defaultTrackUri: "http://url.com/click?a(1556954)p(17269)ttid(13)",
        siteSpecific: false,
        discountAmount: 5,
        isPercentage: false,
        publisherInformation: "",
        languageId: "it",
        exclusive: false,
        currencyId: "EUR",
        logoPath: "http://url.com/file/17269/eDreams_100x50.jpg"
    },
    {
        id: 19699,
        programId: 29519,
            programName: "",
        code: "6G4KGGDH",
        updateDate: "1369813973159",
        publishStartDate: "1370037600000",
        publishEndDate: "1372629599999",
        startDate: "1370037600000",
        endDate: "1372629599999",
        title: "Sconto",
        shortDescription: "kkkkk",
        description: "kkkk",
        voucherTypeId: 1,
        defaultTrackUri: "http://url.com/click?a(1556954)p(17269)ttid(13)",
        siteSpecific: false,
        discountAmount: 5,
        isPercentage: false,
        publisherInformation: "",
        languageId: "it",
        exclusive: false,
        currencyId: "EUR",
        logoPath: "http://url.com/file/17269/eDreams_100x50.jpg"
    }
]

2 个答案:

答案 0 :(得分:1)

来自Tradedoubler的JSON无效。问题是它没有在对象属性名称周围加上双引号,只有值。

以此为例:

$json = '[
    {
        "id": 20108,
        "programId": 17269,
        "programName": "",
        "code": "6G4KGGDH"
    },
    {
        "id": 19699,
        "programId": 29519,
        "programName": "",
        "code": "6G4KGGDH"
    }
]';

print_r(json_decode($json));

在上面的JSON中,我添加了必要的双引号,如果你运行该代码,你会看到它会成功输出:

Array
(
    [0] => stdClass Object
        (
            [id] => 20108
            [programId] => 17269
            [programName] => 
            [code] => 6G4KGGDH
        )

    [1] => stdClass Object
        (
            [id] => 19699
            [programId] => 29519
            [programName] => 
            [code] => 6G4KGGDH
        )

)

因此,您有两种选择:

  1. 联系Tradedoubler并请求他们提供有效的JSON
  2. 解析缺少引号的属性名称并插入它们,这将更正原始无效的JSON。

  3. 如果您想测试JSON的片段是否有效,可以将其粘贴到jsonlint.com,它会告诉您哪里有错误。如果你从tradedoubler粘贴你的JSON,它确实会告诉你它是无效的。

答案 1 :(得分:0)

最后我有解决方案:Tradedoubler还提供了xml版本的凭证文件,所以使用curl你可以解码它:

    <?php

$url="http://api.tradedoubler.com/1.0/vouchers?token=YOURTOKEN";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents

$data = curl_exec($ch); // execute curl request
curl_close($ch);

$xml = simplexml_load_string($data);
print_r($xml);

?>

并循环结果:

<?php

$url="http://api.tradedoubler.com/1.0/vouchers?token=YOUR TOKEN HERE";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents

$data = curl_exec($ch); // execute curl request
curl_close($ch);

$xml = simplexml_load_string($data);
foreach ($xml->voucher as $coupon){
echo '<li>'.$coupon->id.' - '.$coupon->programId.' - '.$coupon->programName.' - '.$coupon->code.' - '.$coupon->title.' - '.$coupon->shortDescription.' - '.$coupon->description.'</li>';
}

?>