如何在smarty模板中解析/解码JSON对象? [英] How to parse/decode JSON object in smarty template?

查看:522
本文介绍了如何在smarty模板中解析/解码JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code in my template file:

{foreach from=$items item=entry}
  <pre>
    {$entry->nb_persons|@print_r}
  </pre>
{/foreach}

输出为(json字符串):

The output is (json string):

{"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"}

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

I would like to print each element seperated, for example :

{$entry->nb_persons.company}

应该给我->"dsad"

Should give me -> "dsad"

但是这不起作用,我不确定为什么.

But this is not working and I'm not sure why.

推荐答案

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

JSON string is just string. To access its members you have to create array/object from this string:

{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}

这篇关于如何在smarty模板中解析/解码JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆