PHP json_decode无法正常工作?

时间:2016-04-14 15:14:34

标签: php arrays json symfony

这是我从第三方应用收到的JSON数据:

"{\"transactionTime\":\"2016-04-14T08:03:20-07:00\",\"receipt\":\"X7MLAFBV\",\"transactionType\":\"TEST_SALE\",\"vendor\":\"wpreelpro\",\"role\":\"VENDOR\",\"totalAccountAmount\":40.62,\"paymentMethod\":\"TEST\",\"totalOrderAmount\":3194.58,\"totalTaxAmount\":0.00,\"totalShippingAmount\":0.00,\"currency\":\"INR\",\"orderLanguage\":\"EN\",\"lineItems\":[{\"itemNo\":\"1\",\"productTitle\":\"WPReelPro (Single Site License)\",\"shippable\":false,\"recurring\":false,\"accountAmount\":40.62,\"quantity\":1,\"downloadUrl\":\"http:\/\/ventture.com\/crm\/index.php\/socket\/receipt\/process\/127\/Clickbank\/?utm_nooverride=1&item=1&cbreceipt=X7MLAFBV&time=1460646200&cbpop=43292B5E&cbaffi=0&cname=Krishna+Ghodke&cemail=krishna%40invanto.com&ccountry=IN&czip=411045&userid=2&product_id=1&gateway_id=37\"}],\"customer\":{\"shipping\":{\"firstName\":\"KRISHNA\",\"lastName\":\"GHODKE\",\"fullName\":\"Krishna Ghodke\",\"email\":\"krishna@invanto.com\",\"address\":{\"postalCode\":\"411045\",\"country\":\"IN\"}},\"billing\":{\"firstName\":\"KRISHNA\",\"lastName\":\"GHODKE\",\"fullName\":\"Krishna Ghodke\",\"email\":\"krishna@invanto.com\",\"address\":{\"postalCode\":\"411045\",\"country\":\"IN\"}}},\"version\":6.0,\"attemptCount\":1,\"vendorVariables\":{\"gateway_id\":\"37\",\"product_id\":\"1\",\"userid\":\"2\",\"utm_nooverride\":\"1\"}}”

然后我在这个JSON上运行了以下代码:

$order = json_decode($decrypted, TRUE);

这给了:

{"transactionTime":"2016-04-14T08:03:20-07:00","receipt":"X7MLAFBV","transactionType":"TEST_SALE","vendor":"wpreelpro","role":"VENDOR","totalAccountAmount":40.62,"paymentMethod":"TEST","totalOrderAmount":3194.58,"totalTaxAmount":0,"totalShippingAmount":0,"currency":"INR","orderLanguage":"EN","lineItems":[{"itemNo":"1","productTitle":"WPReelPro (Single Site License)","shippable":false,"recurring":false,"accountAmount":40.62,"quantity":1,"downloadUrl":"http:\/\/ventture.com\/crm\/index.php\/socket\/receipt\/process\/127\/Clickbank\/?utm_nooverride=1&item=1&cbreceipt=X7MLAFBV&time=1460646200&cbpop=43292B5E&cbaffi=0&cname=Krishna+Ghodke&cemail=krishna%40invanto.com&ccountry=IN&czip=411045&userid=2&product_id=1&gateway_id=37"}],"customer":{"shipping":{"firstName":"KRISHNA","lastName":"GHODKE","fullName":"Krishna Ghodke","email":"krishna@invanto.com","address":{"postalCode":"411045","country":"IN"}},"billing":{"firstName":"KRISHNA","lastName":"GHODKE","fullName":"Krishna Ghodke","email":"krishna@invanto.com","address":{"postalCode":"411045","country":"IN"}}},"version":6,"attemptCount":1,"vendorVariables":{"gateway_id":"37","product_id":"1","userid":"2","utm_nooverride":"1”}}

现在,我希望将值提取为$ order [0] - > transactionTime(这是上面数组中的第一个变量)。但它不起作用。我错过了什么?

3 个答案:

答案 0 :(得分:-1)

这是我对我的问题的回答:

<?php
$data = '[{"transactionTime":"2016-04-14T05:48:41-07:00","receipt":"AFQ95QHZ","transactionType":"TEST_SALE","vendor":"wpreelpro","role":"VENDOR","totalAccountAmount":40.62,"paymentMethod":"TEST","totalOrderAmount":3194.58,"totalTaxAmount":0,"totalShippingAmount":0,"currency":"INR","orderLanguage":"EN","lineItems":[{"itemNo":"1","productTitle":"WPReelPro (Single Site License)","shippable":false,"recurring":false,"accountAmount":40.62,"quantity":1,"downloadUrl":"http:\/\/ventture.com\/crm\/index.php\/socket\/receipt\/process\/127\/Clickbank\/?utm_nooverride=1&item=1&cbreceipt=AFQ95QHZ&time=1460638121&cbpop=8C0BB1D7&cbaffi=0&cname=Krishna+Ghodke&cemail=krishna%40invanto.com&ccountry=IN&czip=411045&userid=2&product_id=1&gateway_id=37"}],"customer":{"shipping":{"firstName":"KRISHNA","lastName":"GHODKE","fullName":"Krishna Ghodke","email":"krishna@invanto.com","address":{"postalCode":"411045","country":"IN"}},"billing":{"firstName":"KRISHNA","lastName":"GHODKE","fullName":"Krishna Ghodke","email":"krishna@invanto.com","address":{"postalCode":"411045","country":"IN"}}},"version":6,"attemptCount":1,"vendorVariables":{"gateway_id":"37","product_id":"1","userid":"2","utm_nooverride":"1"}}]';
 $json = json_decode($data);

  parse_str($json[0]->lineItems[0]->downloadUrl, $cdata);
  echo"<pre>"; var_dump($cdata); exit;
?>

答案 1 :(得分:-2)

首先,数组中没有“订单”。你的json不包含对象的数组集合,但只有一个对象,这就是无法通过$order[0]

访问它的原因

检查以下代码

$str = "{\"transactionTime\":\"2016-04-14T08:03:20-07:00\",\"receipt\":\"X7MLAFBV\",\"transactionType\":\"TEST_SALE\",\"vendor\":\"wpreelpro\",\"role\":\"VENDOR\",\"totalAccountAmount\":40.62,\"paymentMethod\":\"TEST\",\"totalOrderAmount\":3194.58,\"totalTaxAmount\":0.00,\"totalShippingAmount\":0.00,\"currency\":\"INR\",\"orderLanguage\":\"EN\",\"lineItems\":[{\"itemNo\":\"1\",\"productTitle\":\"WPReelPro (Single Site License)\",\"shippable\":false,\"recurring\":false,\"accountAmount\":40.62,\"quantity\":1,\"downloadUrl\":\"http:\/\/ventture.com\/crm\/index.php\/socket\/receipt\/process\/127\/Clickbank\/?utm_nooverride=1&item=1&cbreceipt=X7MLAFBV&time=1460646200&cbpop=43292B5E&cbaffi=0&cname=Krishna+Ghodke&cemail=krishna%40invanto.com&ccountry=IN&czip=411045&userid=2&product_id=1&gateway_id=37\"}],\"customer\":{\"shipping\":{\"firstName\":\"KRISHNA\",\"lastName\":\"GHODKE\",\"fullName\":\"Krishna Ghodke\",\"email\":\"krishna@invanto.com\",\"address\":{\"postalCode\":\"411045\",\"country\":\"IN\"}},\"billing\":{\"firstName\":\"KRISHNA\",\"lastName\":\"GHODKE\",\"fullName\":\"Krishna Ghodke\",\"email\":\"krishna@invanto.com\",\"address\":{\"postalCode\":\"411045\",\"country\":\"IN\"}}},\"version\":6.0,\"attemptCount\":1,\"vendorVariables\":{\"gateway_id\":\"37\",\"product_id\":\"1\",\"userid\":\"2\",\"utm_nooverride\":\"1\"}}";

// Decode as object
$object = json_decode(stripslashes($str));
echo $object->transactionTime;

// Decode as array
$array = json_decode(stripslashes($str), true);
echo $array['transactionTime'];

答案 2 :(得分:-4)

来自manual assoc ,如果为TRUE,则返回的对象将转换为关联数组。

您已将其设置为true,因此现在您有一个关联数组,并且您需要像访问任何其他数组一样访问键/值。但由于JSON已被双重编码,因此您需要使用json_decode()将其换行两次:

$decrypted = '"{\"transactionTime\":\"2016-04-14T08:03:20-07:00\",\"receipt\":\"X7MLAFBV\",\"transactionType\":\"TEST_SALE\",\"vendor\":\"wpreelpro\",\"role\":\"VENDOR\",\"totalAccountAmount\":40.62,\"paymentMethod\":\"TEST\",\"totalOrderAmount\":3194.58,\"totalTaxAmount\":0.00,\"totalShippingAmount\":0.00,\"currency\":\"INR\",\"orderLanguage\":\"EN\",\"lineItems\":[{\"itemNo\":\"1\",\"productTitle\":\"WPReelPro (Single Site License)\",\"shippable\":false,\"recurring\":false,\"accountAmount\":40.62,\"quantity\":1,\"downloadUrl\":\"http:\/\/ventture.com\/crm\/index.php\/socket\/receipt\/process\/127\/Clickbank\/?utm_nooverride=1&item=1&cbreceipt=X7MLAFBV&time=1460646200&cbpop=43292B5E&cbaffi=0&cname=Krishna+Ghodke&cemail=krishna%40invanto.com&ccountry=IN&czip=411045&userid=2&product_id=1&gateway_id=37\"}],\"customer\":{\"shipping\":{\"firstName\":\"KRISHNA\",\"lastName\":\"GHODKE\",\"fullName\":\"Krishna Ghodke\",\"email\":\"krishna@invanto.com\",\"address\":{\"postalCode\":\"411045\",\"country\":\"IN\"}},\"billing\":{\"firstName\":\"KRISHNA\",\"lastName\":\"GHODKE\",\"fullName\":\"Krishna Ghodke\",\"email\":\"krishna@invanto.com\",\"address\":{\"postalCode\":\"411045\",\"country\":\"IN\"}}},\"version\":6.0,\"attemptCount\":1,\"vendorVariables\":{\"gateway_id\":\"37\",\"product_id\":\"1\",\"userid\":\"2\",\"utm_nooverride\":\"1\"}}"';
$order = json_decode (json_decode($decrypted), TRUE ); // double-encoded so you need to double-decode

echo $order['transactionTime']; // 016-04-14T08:03:20-07:00

如果必须使用返回的JSON作为对象,则需要从TRUE函数调用中删除json_decode()

$decrypted = '"{\"transactionTime\":\"2016-04-14T08:03:20-07:00\",\"receipt\":\"X7MLAFBV\",\"transactionType\":\"TEST_SALE\",\"vendor\":\"wpreelpro\",\"role\":\"VENDOR\",\"totalAccountAmount\":40.62,\"paymentMethod\":\"TEST\",\"totalOrderAmount\":3194.58,\"totalTaxAmount\":0.00,\"totalShippingAmount\":0.00,\"currency\":\"INR\",\"orderLanguage\":\"EN\",\"lineItems\":[{\"itemNo\":\"1\",\"productTitle\":\"WPReelPro (Single Site License)\",\"shippable\":false,\"recurring\":false,\"accountAmount\":40.62,\"quantity\":1,\"downloadUrl\":\"http:\/\/ventture.com\/crm\/index.php\/socket\/receipt\/process\/127\/Clickbank\/?utm_nooverride=1&item=1&cbreceipt=X7MLAFBV&time=1460646200&cbpop=43292B5E&cbaffi=0&cname=Krishna+Ghodke&cemail=krishna%40invanto.com&ccountry=IN&czip=411045&userid=2&product_id=1&gateway_id=37\"}],\"customer\":{\"shipping\":{\"firstName\":\"KRISHNA\",\"lastName\":\"GHODKE\",\"fullName\":\"Krishna Ghodke\",\"email\":\"krishna@invanto.com\",\"address\":{\"postalCode\":\"411045\",\"country\":\"IN\"}},\"billing\":{\"firstName\":\"KRISHNA\",\"lastName\":\"GHODKE\",\"fullName\":\"Krishna Ghodke\",\"email\":\"krishna@invanto.com\",\"address\":{\"postalCode\":\"411045\",\"country\":\"IN\"}}},\"version\":6.0,\"attemptCount\":1,\"vendorVariables\":{\"gateway_id\":\"37\",\"product_id\":\"1\",\"userid\":\"2\",\"utm_nooverride\":\"1\"}}"';
$order = json_decode (json_decode($decrypted) ); // do not set assoc to TRUE; leave as default FALSE; double-encoded so you need to double-decode

echo $order->transactionTime; // 2016-04-14T08:03:20-07:00