Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP json_解码无法正常工作?_Php_Arrays_Json_Symfony - Fatal编程技术网

PHP json_解码无法正常工作?

PHP json_解码无法正常工作?,php,arrays,json,symfony,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\",\"totalOrderAmou

这是我从第三方应用程序收到的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\"}}”
$order = json_decode($decrypted, TRUE);
然后我在这个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\"}}”
$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(这是上面数组中的第一个变量)。但它不起作用。我遗漏了什么?

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

您已经将其设置为true,因此现在您有了一个关联数组,并且需要像访问任何其他数组一样访问keys/vals。但是由于
JSON
是双重编码的,因此需要用
JSON\u decode()将其包装两次:

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

$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
首先,数组中没有“订单”。您的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'];

以下是我的问题的答案:

<?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;
?>


您的JSON是双重编码的。一旦对它进行解码,就会得到一个JSON字符串,您需要再次对其进行解码。或者一开始就注意不要对它进行两次编码!我认为$order['transactionTime']将是一个不错的选择。但是,我知道什么?为什么要使用
[0]
?JSON看起来像一个对象,而不是一个数组。在设置选项/参数之前,你需要阅读手册,而不知道它们的实际用途:将assoc设置为
TRUE
返回一个关联数组,而不是一个对象。还有“这给了:”当你做了什么?不是我(我因为一个完美的工作示例而被杀了),但是我的猜测是因为返回的
JSON
是双引号编码的,所以您的
$str
需要用单引号包装,以确保完整的
JSON
响应的完整性(响应用这些双引号包装,但您使用它们将响应包装为字符串),例如
$str='{交易时间\“:…}”很容易错过。OP应该解释得更好。@Marcus看起来他使用的是
stripslashes
,而不是双重解码。在这种情况下,双重解码确实有效。检查下面我的答案。他正在从第三方应用程序接收已经双重编码的
JSON
。但他所做的只是一次解码,留下一个字符串,他无法访问其中的键/值,原因很明显。@KrishnaGhodke它正在工作,但你创建了你的答案并接受了它。这有什么意义呢?请所有看到这个问题的人帮忙,至少解释一下这里的反对票。