Api Platform-Symfony-以持久功能返回json

时间:2019-03-22 17:33:56

标签: php symfony

我需要在持久功能内返回一个JsonResponse。

这是我的DataPersister类的示例,目标是返回JsonResponse,当我尝试时,出现错误:控制器必须返回一个\“ Symfony \ Component \ HttpFoundation \ Response \”对象,但它返回了一个对象类型为App \ Entity \ VerificationCodes。

game_hash = {
    :home => 
    {
        :team_name => "Brooklyn Nets",
        :colors => ["Black", "White"],
        :players => {
            :player1 => {
                :name => "Alan Anderson",
                :number => 0,
                :shoe_size => 16,
                :points => 22,
                :rebounds => 12,
                :assists => 12,
                :steals => 3,
                :blocks => 1,
                :slam_dunks => 1
            },
            :player2 => {
                :name => "Reggie Evans",
                :number => 30,
                :shoe_size => 14,
                :points => 12,
                :rebounds => 12,
                :assists => 12,
                :steals => 12,
                :blocks => 12,
                :slam_dunks => 7
            },
            :player3 => {
                :name => "Brook Lopez",
                :number => 11,
                :shoe_size => 17,
                :points => 17,
                :rebounds => 19,
                :assists => 10,
                :steals => 3,
                :blocks => 1,
                :slam_dunks => 15
            },
            :player4 => {
                :name => "Mason Plumlee",
                :number => 1,
                :shoe_size => 19,
                :points => 26,
                :rebounds => 12,
                :assists => 6,
                :steals => 3,
                :blocks => 8,
                :slam_dunks => 5
            },
            :player5 => {
                :name => "Jason Terry",
                :number => 31,
                :shoe_size => 15,
                :points => 19,
                :rebounds => 2,
                :assists => 2,
                :steals => 4,
                :blocks => 11,
                :slam_dunks => 1

            }
    },      

    :away =>
    {
        :team_name => "Charlotte Hornets",
        :colors => ["Turquoise", "Purple"],
        :players => {
            :player1 => {
                :name => "Jeff Adrien",
                :number => 4,
                :shoe_size => 18,
                :points => 10,
                :rebounds => 1,
                :assists => 1,
                :steals => 2,
                :blocks => 7,
                :slam_dunks => 2
            },
            :player2 => {
                :name => "Bismak Biyombo",
                :number => 0,
                :shoe_size => 16,
                :points => 12,
                :rebounds => 4,
                :assists => 7,
                :steals => 7,
                :blocks => 15,
                :slam_dunks => 10
            },
            :player3 => {
                :name => "DeSagna Diop",
                :number => 2,
                :shoe_size => 14,
                :points => 24,
                :rebounds => 12,
                :assists => 12,
                :steals => 4,
                :blocks => 5,
                :slam_dunks => 5
            },
            :player4 => {
                :name => "Ben Gordon",
                :number => 8,
                :shoe_size => 15,
                :points => 33,
                :rebounds => 3,
                :assists => 2,
                :steals => 1,
                :blocks => 1,
                :slam_dunks => 0
            },
            :player5 => {
                :name => "Brendon Haywood",
                :number => 33,
                :shoe_size => 15,
                :points => 6,
                :rebounds => 12,
                :assists => 12,
                :steals => 22,
                :blocks => 5,
                :slam_dunks => 12
            }
        }
    }
}
}


    ##winning team
def winning_team(game_data)
  #finding the sum of all :points in :home

  game_data[:home].each do |attribute, value|
    value[:players].each do |player, player_data|
      score_1 = player_data.find{|k| k[:points]}.sum
    end 
  end 

  #finding the sum of all :points in :away
  game_data[:away].each do |attribute, value|
    value[:players].each do |player, player_data|
      score_2 = player_data.find{|k| k[:points]}.sum
    end 
  end 

  #outputting a final statement of who won the game


    if score_1 < score_2 
    puts "Brooklyn Nets won the game"
  else 
    puts "Charlotte Hornets won the game"
  end




end

请帮助我或告诉我我能做的比你

1 个答案:

答案 0 :(得分:2)

如果您希望将App\Entity\Users类转换为JSON,则可以实现JsonSerializable接口(PHP本机接口)。该接口迫使您实现方法jsonSerialized,该方法在对象转换为json时会自动调用(例如,当您使用json_encode($user);时。)

最后,您可以尝试

class Users implements \JsonSerializable
{
    public function jsonSerialize()
    {
        return []; // an array representation of your class
    }
}

那么您可以...

return new JsonResponse(json_encode($user));

或(也许)...

return new JsonResponse($user);