PHP: JSON_DECODE 不适用于以下数据

PHP: JSON_DECODE is not working for the following data

我正在尝试通过以下代码解码此 json 数据,但它不起作用。

$data = 'VA_OnQueryData({"name":"John","id":"354902332592"});';
return json_decode($data);

它不起作用,因为它不是 json。试试这个:

$data = '{"name":"John","id":"354902332592"}';
return json_decode($data);

删除 URL 中的 callback 参数。而不是这个:

https://api.vinaudit.com/query.php?key=VA_MAIN&callback=VA_OnQueryData&vin=1C6RR6LT3HS847897

使用这个:

https://api.vinaudit.com/query.php?key=VA_MAIN&vin=1C6RR6LT3HS847897

然后,你就会真正有用JSON。

您使用的方法是 JSON-P, and used to be used to get around cross-domain issues by sending up executable JavaScript. This method is no longer required client-side thanks to CORS,并且从来不需要 server-side。

此外,回到制作此 API 的人并告诉他们他们使用了错误的 Content-Type 响应 header。他们正在发送 text/html...应该是 application/json。 (参考:https://whosebug.com/a/477819/362536

您好,您的尝试 json_decode 无效 json。

如果您必须以这种方式使用它,您需要先按如下方式清洁它:

$data = 'VA_OnQueryData({"vin":"1C6RR6LT3HS847897","id":"697015470432","attributes":{"VIN":"1C6RR6LT3HS847897","Year":"2017","Make":"Ram","Model":"1500","Trim":"Lone Star","Made In":"United States","Style":"Crew Cab Pickup (4-Door)","Engine":"5.7L V8 OHV 16V"},"success":true,"error":""});';

preg_match('/{.*}/', $data, $cleaned);

return json_decode($cleaned[0]);

这个输出是:

stdClass Object
(
    [vin] => 1C6RR6LT3HS847897
    [id] => 697015470432
    [attributes] => stdClass Object
        (
            [VIN] => 1C6RR6LT3HS847897
            [Year] => 2017
            [Make] => Ram
            [Model] => 1500
            [Trim] => Lone Star
            [Made In] => United States
            [Style] => Crew Cab Pickup (4-Door)
            [Engine] => 5.7L V8 OHV 16V
        )

    [success] => 1
    [error] => 
)