dongyou6795
2016-04-14 15:14
浏览 26
已采纳

PHP json_decode无法正常工作?

This is the JSON data that I received from a 3rd party app:

"{\"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\"}}”

Then I ran the following code on this JSON:

$order = json_decode($decrypted, TRUE);

This gave:

{"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”}}

Now, I wish to extract the values as $order[0]->transactionTime (which is first variable in array above). But it doesn’t work. What am I missing?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongxia026531 2016-04-14 15:47
    已采纳

    Firstly there are no "orders" in array. Your json does not contain array collection of objects but only one object that is why it's impossible to access it via $order[0]

    Check code below

    $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'];
    
    点赞 打赏 评论
  • dougouqin0763 2016-04-14 15:19

    From the manual: assoc, When TRUE, returned objects will be converted into associative arrays.

    You've set that to true, so now you have an associative array, and you need to access the keys/vals like you would any other array. But since the JSON has been double-encoded, you'll need to wrap it twice with 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
    

    If you must use the returned JSON as an object, you need to remove TRUE from your json_decode() function call:

    $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
    
    点赞 打赏 评论
  • dtcuv8044 2016-04-15 06:04

    Here is i got answer of my question:

    <?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;
    ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题