【问题标题】:JSON response in LaravelLaravel 中的 JSON 响应
【发布时间】:2018-01-14 22:43:46
【问题描述】:

我正在访问一个 API,我从这个 API 得到一个 JSON 响应:

{
    "products": [
        {
            "id": 455968,
            "name": "Nike Satire",
            "shortName": "Nike Satire",
            "priceMin": 199.99,
            "priceMax": 199.99,
            "discount": 0,
            "thumbnail": {
                "url": "https://thumbs.buscape.com.br/tenis/nike-satire_600x600-PU6f520_1.jpg",
                "height": 600,
                "width": 600,
                "otherFormats": [
                    {
                        "url": "https://thumbs.buscape.com.br/tenis/nike-satire_100x100-PU6f520_1.jpg",
                        "height": 100,
                        "width": 100
                    },
                    {
                        "url": "https://thumbs.buscape.com.br/tenis/nike-satire_200x200-PU6f520_1.jpg",
                        "height": 200,
                        "width": 200
                    },
                    {
                        "url": "https://thumbs.buscape.com.br/tenis/nike-satire_300x300-PU6f520_1.jpg",
                        "height": 300,
                        "width": 300
                    },
                    {
                        "url": "https://thumbs.buscape.com.br/tenis/nike-satire_600x600-PU6f520_1.jpg",
                        "height": 600,
                        "width": 600
                    }
                ]
            },
            "hasOffer": 2
        },
        {
            "id": 611192,
            "name": "Nike Air Vapor Ace",
            "shortName": "Nike Air Vapor Ace",
            "priceMin": 229.99,
            "priceMax": 229.99,
            "discount": 0,
            "thumbnail": {
                "url": "https://thumbs.buscape.com.br/tenis/nike-air-vapor-ace_600x600-PU95378_1.jpg",
                "height": 600,
                "width": 600,
                "otherFormats": [
                    {
                        "url": "https://thumbs.buscape.com.br/tenis/nike-air-vapor-ace_100x100-PU95378_1.jpg",
                        "height": 100,
                        "width": 100
                    },
                    {
                        "url": "https://thumbs.buscape.com.br/tenis/nike-air-vapor-ace_200x200-PU95378_1.jpg",
                        "height": 200,
                        "width": 200
                    },
                    {
                        "url": "https://thumbs.buscape.com.br/tenis/nike-air-vapor-ace_300x300-PU95378_1.jpg",
                        "height": 300,
                        "width": 300
                    },
                    {
                        "url": "https://thumbs.buscape.com.br/tenis/nike-air-vapor-ace_600x600-PU95378_1.jpg",
                        "height": 600,
                        "width": 600
                    }
                ]
            },
            "hasOffer": 1
        }
    ]
}

在视图中,我试图将这些信息显示为:

    <div class="container container-margin">
        <div class="list-group">
            @foreach($produtos->products as $t)
            <a href="{{ $t->id }}" class="list-group-item"><img src="">{{ $t->name }} - from {{ money_format('%n',$t->priceMin) }} to {{ money_format('%n',$t->priceMax) }}</a>
            @endforeach
        </div>
    </div>

我的问题是:如何显示缩略图网址?

我尝试了以下方法:

{{ $t->缩略图->url }}

{{ $t['thumbnail']['url'] }}

@foreach($t->thumbnail as $v) {{ $v->url }} @endforeach

当试图得到一个孩子的孩子时,似乎没有任何效果。

【问题讨论】:

  • 执行var_dump($t) 并检查它实际包含的内容。就像之前的评论一样,给我们更多关于实际发生了什么的信息..
  • 未定义属性:stdClass::$url;不能将 stdClass 类型的对象用作数组;试图获得非对象的属性。这是我得到的错误。
  • 你能发布你的控制器代码吗?

标签: php json laravel-5


【解决方案1】:

您应该将 json 数据转换为数组,然后根据需要使用它。 在此处查看更多详细信息:

Laravel: How do I parse this json data in view blade?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-16
    • 2021-06-05
    • 2015-10-30
    • 2014-11-11
    • 2015-05-17
    • 2020-03-04
    • 2017-09-24
    • 1970-01-01
    相关资源
    最近更新 更多