2016-03-10 54 views
2

我有一個嵌套的JSON文件顯示嵌套的JSON在laravel 5.2

{ 
     "event_type": "INCOMING_BTC", 
     "event_uid": "5515c5601f7b3", 
     "datetime": "2015-03-27 21:02:37", 
     "resources": [ 
      { 
       "resource_type": "transaction", 
       "resource": { 
        "id": 105062, 
        "datetime": "2015-03-27 21:02:23", 
        "description": "Money from Xapo Tip", 
        "order_type": "payment_received", 
        "from": { 
         "type": "btc_address", 
         "id": "1AqF787aPHgPRZ81kdQSeEwW46yjyrAaxR" 
        }, 
        "to": { 
         "destination_type": "btc_address", 
         "destination_id": "1N65Bz88zKUDPKhUUsx8f9Qwsuo96Hqz7S", 
         "to_account": 1276 
        }, 
        "generic_type": "credit", 
        "amount": "0.0000001", 
        "currency": "BTC", 
        "status": "completed", 
        "txConfidence": 1, 
        "rejection_reason": null, 
        "notes": null, 
        "base_currency": "USD", 
        "exchange_rate": null, 
        "exchange_amount": null 
       } 
      }, 
      { 
       "resource_type": "address", 
       "resource": { 
        "id": "1N65Bz88zKUDPKhUUsx8f9Qwsuo96Hqz7S", 
        "address": "1N65Bz88zKUDPKhUUsx8f9Qwsuo96Hqz7S", 
        "meta_data": null, 
        "label": null, 
        "total_received": "0.00000350", 
        "created_at": "2015-03-08 14:50:59", 
        "address_type": "multisig", 
        "id_account": "1276" 
       } 
      } 
     ] 
    } 

而且我保存名稱公用文件夾在該文件中xapojson.txt

在我的路線文件我已經做json_decode解碼這個數據給一個變量「交易」,並通過它來查看

Route::get('/', function() { 
    $transaction = json_decode(file_get_contents('xapojson.txt')); 
    return view('transaction', compact('transaction')); 
}); 

現在,在交易視圖我不得不從這個嵌套JSON顯示數據。 我已經嘗試了很多變化,並搜索谷歌和stackoverflow,但沒有奏效。 我發現這有點有用Check this out。但它也沒有進入更多的嵌套。

在我來顯示這些DATAS的觀點: -

resources[0]->resource->from->type 

resources[0]->resource->id 

resources[0]->resource->from->id 

resources[0]->resource->status 

resources[0]->resource->amount 

resources[0]->resource->currency 

resources[0]->resource->to->destination_id 

datetime 

請大家幫我上顯示上述領域。

回答

0

好吧,當我在做這個代碼工作簡單的php,我通過

$xapo_json = file_get_contents('xapojson.txt'); 
$xapo_json_decoded = json_decode($xapo_json); 
$from_type = $xapo_json_decoded->resources[0]->resource->from->type; 
$transacid = $xapo_json_decoded->resources[0]->resource->id; 
$from = $xapo_json_decoded->resources[0]->resource->from->id; 
$status = $xapo_json_decoded->resources[0]->resource->status; 
$amount = $xapo_json_decoded->resources[0]->resource->amount; 
$currency = $xapo_json_decoded->resources[0]->resource->currency; 
$to = $xapo_json_decoded->resources[0]->resource->to->destination_id; 
$datetime = $xapo_json_decoded->datetime; 

然後提取的數據中的路由文件I改變

return view('transaction', compact('transaction')); 

return View::make('transaction')->with('transaction',$transaction); 

,並鑑於我用這個

瞧,它的工作原理

0

你可以用下面的代碼做(正在服用格爲例,你可以把它改成表或任何

@foreach($transaction->resources as $resource) 
@if(is_set($resource->from)) 
<div>{{$resource->from->type}}</div> 
@endif 
<div>{{$resource->id}}</div> 
@if(is_set($resource->from)) 
<div>{{$resource->from->id}}</div> 
@endif 
<div>{{$resource->status}}</div> 

<div>{{$resource->amount}}</div> 

<div>{{$resource->currency}}</div> 

<div>{{$resource->to->destination_id}}</div> 
@endforeach 
+0

嘗試此我得到錯誤後良好。 'ErrorException in 64a884a3b8243906c26295b5c5674b98d081a606.php line 2: 未定義的屬性:stdClass :: $ from(View:/home/yash/website/resources/views/transaction.blade.php)' –

+0

這是因爲有些資源沒有「從......」開始。你可以在顯示之前使用條件查詢我將更新相同的代碼段 –

+0

同樣適用於其他人以及json響應對每個資源都有不同的值集合希望您瞭解代碼中的邏輯 –

0

嘗試使用

$transaction = json_decode(file_get_contents('xapojson.txt'), true);