本文介绍了如何在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):

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

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

应该给我->"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对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:24