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

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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!