如何在smarty模板中解析/解码JSON对象?

coo*_*669 6 php json smarty smarty3

我的模板文件中有以下代码:

{foreach from=$items item=entry}
  <pre>
    {$entry->nb_persons|@print_r}
  </pre>
{/foreach}
Run Code Online (Sandbox Code Playgroud)

输出是(json字符串):

{"ip":"12.12.12.12","date":1375616434,"cartitems":["foo:1"],"company":"dsad","FirstName":"sad","LastName":"asdsad","street":"","postcode":"","city":"","country":"Andorra","phone":"456456","fax":"","email":"sad@sad.com","comefrom":"google","request":"","message":"sadads"}

我想打印每个分开的元素,例如:

{$entry->nb_persons.company}

应该给我 - >"dsad"

但这不起作用,我不知道为什么.

dev*_*ler 12

JSON字符串只是字符串.要访问其成员,您必须从此字符串创建数组/对象:

{foreach from=$items item=entry}
  {* create array from JSON string*}
  {assign var=person value=$entry->nb_persons|json_decode:1}
  <pre>
    {$person.company}
  </pre>
{/foreach}
Run Code Online (Sandbox Code Playgroud)