如何在Laravel中获取Json数据

时间:2016-11-02 12:43:45

标签: json laravel

我有角度前端的JSON响应。回应是:

var results = (from t in CurrentDataSource.Table1
               where t.Manager == userId && t.Profile != null
               select t.Profile.GetValueOrDefault()).ToList();

我想在laravel中获取所有卡片名称。在laravel控制器功能中我的代码如下:它返回一些错误。我不知道如何获取所有这些var element = $('.banner').find('h1'); // or other container... if (element.length) $('h1').not(element).remove();

 {
  "selected": null,
  "lists": [
    {
      "board one": [
        {
          "card_name": "Card 10",
          "id": 1,
          "card_unique_id": "PC1"
        },
        {
          "card_name": "card 11",
          "id": 2,
          "card_unique_id": "PC2"
        },
        {
          "card_name": "card12",
          "id": 3,
          "card_unique_id": "PC3"
        },
        {
          "card_name": "adfaf",
          "id": 12,
          "card_unique_id": "PC12"
        },
        {
          "card_name": "fa df af ad",
          "id": 13,
          "card_unique_id": "PC13"
        }
      ],
      "BoardId": "PB1"
    },
    {
      "board two": [
        {
          "card_name": "Card 20",
          "id": 4,
          "card_unique_id": "PC4"
        },
        {
          "card_name": "CArd 21",
          "id": 5,
          "card_unique_id": "PC5"
        },
        {
          "card_name": "card 23",
          "id": 6,
          "card_unique_id": "PC6"
        },
        {
          "card_name": "Card 24",
          "id": 7,
          "card_unique_id": "PC7"
        }
      ],
      "BoardId": "PB2"
    },
    {
      "board three": [
        {
          "card_name": "Card 30",
          "id": 8,
          "card_unique_id": "PC8"
        },
        {
          "card_name": "card 31",
          "id": 9,
          "card_unique_id": "PC9"
        },
        {
          "card_name": "card 32",
          "id": 10,
          "card_unique_id": "PC10"
        }
      ],
      "BoardId": "PB3"
    },
    {
      "new board": [
        {
          "card_name": "card 40",
          "id": 11,
          "card_unique_id": "PC11"
        }
      ],
      "BoardId": "PB4"
    }
  ]
}

请任何人帮忙。 谢谢高级..

1 个答案:

答案 0 :(得分:3)

true添加到json_decode()以获取关联数组:

$get_result_arr = json_decode($response->getContent(), true);

然后遍历此数组并获取每个card_name

foreach ($get_result_arr['lists'] as $list) {
    foreach (array_values($list)[0] as $card) {
        echo $card['card_name'];
    }
}