dqq22391 2015-01-21 13:21
浏览 59
已采纳

转换Symfony2 PHP实体对象以在Javascript中使用

I'm attempting to open up data from a PHP Object (as shown below) but I'd like to be able to access this data within JavaScript to use in a graphing library.

Object in question:

http://puu.sh/eP3QZ/e4289eb0d8.png

What I need to be able to do is convert that into a JSON encoded object for use within Javascript.

I tried using twig within Symfony to do this via:

{% set playerStats = match.getStatsPlayers().getValues() }%
{% dump(playerStats) %} // This is what you see above

var playerStats = {{ playerStats|json_encode }};

console.log(playerStats);

The console shows this:

http://puu.sh/eP4aX/adf6c9978f.png

Which is where I'm banging my head against the wall. Where can I access the values of these properties?

As an inefficient way, I've managed to get it into a JavaScript object by doing:

{% for p in playerStats %}
    playerStats.push({ 'id': {{p.playerID}}, 'playerName': '{{p.playerName}}', 'playerOutfit': {{p.playerOutfit}}, 'playerFaction': {{p.playerFaction}}, 'playerKills': {{p.playerKills}}, 'playerDeaths': {{p.playerDeaths}}, 'playerTeamKills': {{p.playerTeamKills}}, 'playerSuicides': {{p.playerSuicides}} });
{% endfor %}

I feel kinda dirty for doing that. There must be a better way to do this surely?

Thanks in advance!

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 如何修改为正确的?求解决
      • ¥15 django访问管理员界面问题
      • ¥20 python homework完成
      • ¥20 求解 多变量系统的最小二乘辨识问题的推导以及matlab仿真
      • ¥15 arduino esp8266 Blinker编译报问题
      • ¥15 ubuntu18.04运行模型,直接死机
      • ¥30 (问卷调查)莫名其妙丢了u盘,你们会是什么心理状态
      • ¥100 Spark+android应⽤案例
      • ¥15 yolov8 目标检测 重叠 遮挡
      • ¥20 微信聊天记录如何部署到服务器上