PHP获取JSON值问题

时间:2016-12-19 11:26:33

标签: php json

我从Webhook收到一些JSON POST数据到我的服务器。我可以按如下方式获取JSON数据:

$orderJSON = file_get_contents('php://input');

返回此JSON:

{
  "order": {
"billing_address": {
  "address_1": "76 Pacific Drive", 
  "address_2": "", 
  "city": "Bondi", 
  "company": "Hanson Media", 
  "country": "AU", 
  "email": "testing@gmail.com", 
  "first_name": "Hansel", 
  "last_name": "Gretten", 
  "phone": "212 554 7855", 
  "postcode": "2026", 
  "state": "NSW"
}, 
"cart_tax": "3.60", 
"completed_at": "2016-12-19T11:07:15Z", 
"coupon_lines": [], 
"created_at": "2016-12-19T11:07:15Z", 
"currency": "AUD", 
"customer": {
  "billing_address": {
    "address_1": "76 Pacific Drive", 
    "address_2": "", 
    "city": "Bondi", 
    "company": "Hanson Media", 
    "country": "AU", 
    "email": "testing@gmail.com", 
    "first_name": "Hansel", 
    "last_name": "Gretten", 
    "phone": "212 554 7855", 
    "postcode": "2026", 
    "state": "NSW"
  }, 
  "email": "testing@gmail.com", 
  "first_name": "Hansel", 
  "id": 0, 
  "last_name": "Gretten", 
  "shipping_address": {
    "address_1": "76 Pacific Drive", 
    "address_2": "", 
    "city": "Bondi", 
    "company": "Hanson Media", 
    "country": "AU", 
    "first_name": "Hansel", 
    "last_name": "Gretten", 
    "postcode": "2026", 
    "state": "NSW"
  }
}, 
"fee_lines": [], 
"id": 3304, 
"is_vat_exempt": false, 
"line_items": [
  {
    "id": 113, 
    "meta": [], 
    "name": "Happy Ninja", 
    "price": "18.00", 
    "product_id": 37, 
    "quantity": 2, 
    "sku": "", 
    "subtotal": "36.00", 
    "subtotal_tax": "3.60", 
    "tax_class": null, 
    "total": "36.00", 
    "total_tax": "3.60"
  }, 
  {
    "id": 114, 
    "meta": [], 
    "name": "Water Bottles", 
    "price": "20.50", 
    "product_id": 3291, 
    "quantity": 1, 
    "sku": "PD885536", 
    "subtotal": "20.50", 
    "subtotal_tax": "0.00", 
    "tax_class": "standard", 
    "total": "20.50", 
    "total_tax": "0.00"
  }
], 
"note": "Call to arrange delivery time", 
"order_key": "wc_order_5857bf639d951", 
"order_number": 3304, 
"payment_details": {
  "method_id": "eway", 
  "method_title": "Credit Card", 
  "paid": false
}, 
"shipping_address": {
  "address_1": "76 Pacific Drive", 
  "address_2": "", 
  "city": "Bondi", 
  "company": "Hanson Media", 
  "country": "AU", 
  "first_name": "Hansel", 
  "last_name": "Gretten", 
  "postcode": "2026", 
  "state": "NSW"
}, 
"shipping_lines": [
  {
    "id": 115, 
    "method_id": "local_pickup:1", 
    "method_title": "Local Pickup", 
    "total": "0.00"
  }
], 
"shipping_methods": "Local Pickup", 
"shipping_tax": "0.00", 
"status": "pending", 
"subtotal": "56.50", 
"tax_lines": [
  {
    "code": "AU-GST-1", 
    "compound": false, 
    "id": 116, 
    "rate_id": "1", 
    "title": "GST", 
    "total": "3.60"
  }
], 
"total": "60.10", 
"total_discount": "0.00", 
"total_line_items_quantity": 3, 
"total_shipping": "0.00", 
"total_tax": "3.60", 
"updated_at": "2016-12-19T11:07:15Z", 
"view_order_url": "https://mywebsite.com/my-account/view-order/3304"
  }
}

我现在需要从JSON获取单个元素,例如我想得到id值(3304)。我试过了:

$orderID = $orderJSON->id;

 $orderID = $orderJSON[id];

但这只会产生错误,例如'尝试获取非对象的属性'。

2 个答案:

答案 0 :(得分:0)

  1. 您需要先使用json_decode(),因为我无法在您的问题中看到这一点。

  2. id位于order家长之下,因此您需要像$orderJSON->order->id

  3. 那样做出声音
  4. 您还必须检查已解码的$orderJSON是否为空

  5. 实施例

    $orderJSON = json_decode($orderJSON);
    if (empty($orderJSON)) {
        throw new RuntimeException('Malformed json');
    }
    $orderID = $orderJSON->order->id;
    

答案 1 :(得分:0)

使用json_decode()函数将JSON文件转换为PHP可读数据。

<?php
$json = json_decode($orderJSON);

解码完JSON文件后,可以使用函数print_r()打印数据的精确表示。

<?php
print_r($orderJSON);

这可以帮助您确定文件的形成方式,从而确定显示所需值所需的所有尺寸。

JSON to nice

现在,如果仔细观察,您会发现为了打印您的ID,您需要通过订单维度。

因此,您可能希望使用语法$json->order->id来定位所需的ID。

<?php
echo $json->order->id;

Display the ID from JSON

PHP : JSON

PHP : print_r()