简体   繁体   English

在Laravel 5.2中显示嵌套的json

[英]Displaying nested json in laravel 5.2

I have a nested json file 我有一个嵌套的json文件

{
        "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"
                }
            }
        ]
    }

And I have saved this file in public folder with name xapojson.txt 我已将此文件保存在公用文件夹中,名称为xapojson.txt

In my routes file I have done json_decode to decode this data to a variable 'transaction' and passed it to view 在我的路线文件中,我已完成json_decode将此数据解码为变量“交易”,并将其传递给查看

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

Now in transaction view I have to display the data from this nested json. 现在在事务视图中,我必须显示来自此嵌套json的数据。 I have tried out lots of variation and searched on google and stackoverflow but nothing worked. 我已经尝试了很多变化,并在Google和stackoverflow上进行了搜索,但没有任何效果。 I found this somewhat helpful Check this out . 我发现这个有点用检查了这一点 But it also do not get into more nesting. 但是它也不会进入更多的嵌套。

In the view I have to display these 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

Please help me on displaying the above fields. 请帮助我显示以上字段。

you can do it with the below code ( am taking div as example you can change it to table or whatever 您可以使用以下代码(以div为例,可以将其更改为表或其他格式)

@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

尝试使用

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

Well when I made this code work in simple php, I extracted the data by 好吧,当我使此代码在简单的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;

Then in the routes file i changed 然后在路由文件中我更改了

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

to

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

And in view I used this 鉴于我使用了这个

{{ $transaction->resources[0]->resource->from->id }}

Voila, It works 瞧,行得通

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM