如何访问此JSON信息(PHP)?

时间:2018-07-20 18:29:57

标签: php json rest

我无法访问orders数组中的内容。我目前无济于事,想知道我做错了什么:

$ json对象是来自其他api的json响应。

$orderData = $json['orders'];
  foreach($orderData['orders'] as $order){
}

{  
   "errors":[  

   ],
   "orders":[  
      {  
         "note":"",
         "estimated_shipping_fee":"0.00",
         "payment_method":"PAY_CYBERSOURCE",
         "escrow_amount":"12.95",
         "message_to_seller":"",
         "shipping_carrier":"Singpost - Normal Mail",
         "currency":"SGD",
         "create_time":1532064559,
         "pay_time":1532064618,
         "recipient_address":{  
            "town":"",
            "city":"",
            "name":"Teo",
            "district":"",
            "country":"SG",
            "zipcode":"41253",
            "full_address":"In some street somewhjere",
            "phone":"23154991",
            "state":""
         },
         "days_to_ship":3,
         "tracking_no":"",
         "order_status":"SHIPPED",
         "note_update_time":0,
         "update_time":1532082525,
         "goods_to_declare":false,
         "total_amount":"12.95",
         "service_code":"",
         "country":"SG",
         "actual_shipping_cost":"",
         "cod":false,
         "items":[  
            {  
               "weight":1.0,
               "item_name":"ABC",
               "is_wholesale":false,
               "item_sku":"5123433123",
               "variation_discounted_price":"12.95",
               "variation_id":0,
               "variation_name":"",
               "item_id":1126534500,
               "variation_quantity_purchased":1,
               "variation_sku":"",
               "variation_original_price":"12.95"
            }
         ],
         "ordersn":"3589290984539"
      }
   ]
}

尝试通过json ['orders'] [0] ['payment_method']直接访问变量无效。

2 个答案:

答案 0 :(得分:0)

使用json_decode()将其从json转换为数组;那么您可以轻松地访问它,如果您想使用jquery进行访问,则只需使用jQuery.parseJSON();对其进行转换即可。

答案 1 :(得分:0)

只需使用json_decode来检索对象,然后使用$obj->note即可,也可以将其转换为数组:$array = get_object_vars($obj);,就像在this answer中一样。