【问题标题】:Displaying nested json in laravel 5.2在 laravel 5.2 中显示嵌套的 json
【发布时间】:2016-06-24 22:26:38
【问题描述】:

我有一个嵌套的 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"
                }
            }
        ]
    }

我已将此文件保存在名为 xapojson.txt 的公用文件夹中

在我的路由文件中,我已完成 json_decode 以将此数据解码为变量“事务”并将其传递给查看

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

现在在事务视图中,我必须显示来自这个嵌套 json 的数据。 我已经尝试了很多变体并在 google 和 stackoverflow 上进行了搜索,但没有任何效果。 我发现这有点帮助Check this out。但它也不会进入更多的嵌套。

在视图中我必须显示这些数据:-

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

请帮助我显示上述字段。

【问题讨论】:

    标签: php json laravel laravel-5


    【解决方案1】:

    尝试使用

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

    【讨论】:

      【解决方案2】:

      好吧,当我让这段代码在简单的 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;
      

      然后在路由文件中我改变了

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

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

      我用了这个

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

      瞧,它有效

      【讨论】:

        【解决方案3】:

        你可以用下面的代码来做(我以 div 为例,你可以把它改成 table 或任何东西

        @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
        

        【讨论】:

        • 在尝试这个之后我得到了错误。 ErrorException in 64a884a3b8243906c26295b5c5674b98d081a606.php line 2: Undefined property: stdClass::$from (View: /home/yash/website/resources/views/transaction.blade.php)
        • 这是因为某些资源中没有“来自”。您可以在显示之前使用条件查询,我将更新相同的代码段
        • 同样适用于其他人,并且 json 响应对每个资源都有不同的值集希望您从代码中理解逻辑
        猜你喜欢
        • 2016-08-09
        • 1970-01-01
        • 1970-01-01
        • 2016-07-12
        • 1970-01-01
        • 1970-01-01
        • 2016-02-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多