0

I need to grab some json code; it is starting with [

If I try to loop it, I retrieve

Warning: Invalid argument supplied for foreach()

Can someone help me to decode properly this json? thanks

PHP Code:

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

[
    {
        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"
    }
]
4

2 回答 2

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。

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

于 2013-06-03T11:49:28.503 回答
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>';
}

?> 
于 2014-04-19T13:53:33.267 回答