2016-01-21 108 views
0

我想测试一个模块,我需要重新创建一个JSON如下:如何获取特定格式的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" 
    } 
    ] 
] 
+0

标记您正在使用的语言。 –

+0

这两个代码块与每个代码块都没有关系(变量名称,数组中的键)。除了json_encode没有必要,因为输入一个字符串 – Gavriel

+0

您正尝试使用'billing_info ['billing']'作为对象(' - > customer_id')和数组('['source']')在同一个时间。它可能在php对象中(magic'offsetGet')。但是我担心,你无法在json中重新创建这样的行为。 –

回答

1
$customer = new stdClass(); 
$customer->customer_id=1; 
$customer->source=new stdClass(); 
$customer->source->exp_year=2018; 
$billing_info = array('billing'=>$customer); 

UPDATE1:

只是意识到你想要什么是不可能的:

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

在第1行,你承担$ billing_info [“结算”]是一个对象,而在第二行,你假设它是一个数组。请确定你喜欢哪一个:

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

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

可选[ 'exp_year']也可以

UPDATE2:

请包括有效的JSON!您可以在这里查看:http://jsonlint.com/

+0

'dd($ billing_info ['billing'] - > customer_id);'这是不可访问的。 – user1012181

+0

我更新了答案 – Gavriel

+0

所以我要为此创建一个新类? – user1012181