Laravel 5 JSON/刀片 [英] Laravel 5 JSON / Blade

查看:129
本文介绍了Laravel 5 JSON/刀片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON传递给视图

I am trying to pass a JSON to my view

使用以下代码:

  Route::get('json', function() {
    $path = storage_path() . "/json/dish.json"; 
    $json = json_decode(file_get_contents($path), true);
    return View::make('pages.json')->withJson($json);
  });

使用{{dd($json)}}

我收到这个:

array:1 [▼
  "dish" => array:297 [▼
    0 => array:2 [▶]
1 => array:2 [▶]
2 => array:2 [▶]
3 => array:5 [▶]
...

当我尝试通过以下方式显示$json的内容时:

When I try to display the content of my $json with:

@foreach($json["dish"] as $key => $item)
  {{$item}}
@endforeach

我收到此错误消息:

htmlentities()期望参数1为字符串,为给定的数组(查看:/Users/beyerdynamic/Documents/Developer/dev1/resources/views/pages/json.blade.php)

htmlentities() expects parameter 1 to be string, array given (View: /Users/beyerdynamic/Documents/Developer/dev1/resources/views/pages/json.blade.php)

我在做什么错了?

推荐答案

默认情况下,Laravel尝试在输入之前转义任何变量.如果要避免它,请使用{!! $item !!}.但是,$ item是数组,它不会帮助您显示正确的值.您将获得"Array"作为输出.如果要显示正确的数据,请使用{{ $item['your_key'] }}.

By default Laravel tries to escape any variables before input. If you want to avoid it use {!! $item !!}. However, $item is array and it will not help you to show proper values. You will get 'Array' as output. If you want to display correct data use {{ $item['your_key'] }}.

这篇关于Laravel 5 JSON/刀片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆