簡體   English   中英

如何獲取特定格式的JSON響應?

[英]How to get the JSON response in a specific format?

我正在嘗試測試一個模塊,我需要重新創建一個json,如下所示:

$billing_info['billing']->customer_id
$billing_info['billing']['source']->exp_year

我嘗試重新創建它,如下所示:

$arr = json_encode(['id'=>'card_17LHmCI49Oxjrr81SringkGq', 'exp_year'=>2018, 'exp_month' => 07, 'funding' => 'credit', 'brand' => 'Visa', 'last4' => '4242']);

$customer = json_encode(['customer_id' => 'cus_7aSheljul3mkAB', 'source' =>json_decode($arr)]);

但是我無法調用dd($billing_info['billing']->customer_id);

所需的JSON響應如下所示:

[
  "billing" => [
    {"customer_id": 12 }
    "source" => {
      "exp_year": 2018
      "exp_month": 7
      "funding": "credit"
      "brand": "Visa"
      "last4": "4242"
    }
  ]
]
$customer = new stdClass();
$customer->customer_id=1;
$customer->source=new stdClass();
$customer->source->exp_year=2018;
$billing_info = array('billing'=>$customer);

更新1:

剛剛意識到您想要的是不可能的:

$billing_info['billing']->customer_id
$billing_info['billing']['source']->exp_year

在第一行中,您假定$ billing_info ['billing']是一個對象,而在第二行中,您假定它是一個數組。 請決定您喜歡哪一個:

$billing_info['billing']->customer_id
$billing_info['billing']->source->exp_year

$billing_info['billing']['customer_id']
$billing_info['billing']['source']->exp_year

也可以選擇['exp_year']

更新2:

請包含一個有效的json! 您可以在這里檢查: http : //jsonlint.com/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM