防止使用PHP json_encode()引用某些值 [英] Prevent quoting of certain values with PHP json_encode()

查看:240
本文介绍了防止使用PHP json_encode()引用某些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP的json_encode将数组编码为JSON字符串时,是否有任何方法可以防止该函数在返回的字符串中引用特定值?我问的原因是因为我需要javascript才能将对象中的某些值解释为实际的变量名称,例如现有javascript函数的名称.

When using PHP's json_encode to encode an array as a JSON string, is there any way at all to prevent the function from quoting specific values in the returned string? The reason I ask is because I need javascript to interpret certain values in the object as actual variable names, for example the name of an existing javascript function.

我的最终目标是使用输出的json作为ExtJS Menu组件的配置对象,因此所有引用都被加为事实,这使我无法成功设置子项的处理程序"(单击事件处理程序功能)之类的属性.数组.

My end goal is to use the outputted json as the configuration object for an ExtJS Menu component, so the fact that everything gets quoted prevents me from successfully setting such properties as "handler" (click event handler function) of the child items arrays.

推荐答案

这就是我最终要做的,这与我认为的Stefan上面的建议非常接近:

This is what I ended up doing, which is pretty close to what Stefan suggested above I think:

class JSObject
{
    var $jsexp = 'JSEXP:';

    /**
     * Encode object
     * 
     * 
     * @param     array   $properties
     * @return    string 
     */
    function encode($properties=array())
    {
        $output    = '';
        $enc_left  = $this->is_assoc($properties) ? '{' : '[';
        $enc_right = ($enc_left == '{') ? '}' : ']';

        foreach($properties as $prop => $value)
        {
            //map 'true' and 'false' string values to their boolean equivalent
            if($value === 'true')  { $value = true; }
            if($value === 'false') { $value = false; }

            if((is_array($value) && !empty($value)) || (is_string($value) && strlen(trim(str_replace($this->jsexp, '', $value))) > 0) || is_int($value) || is_float($value) || is_bool($value))
            {
                $output .= (is_string($prop)) ? $prop.': ' : '';

                if(is_array($value))
                {
                    $output .= $this->encode($value);
                }
                else if(is_string($value))
                {
                    $output .= (substr($value, 0, strlen($this->jsexp)) == $this->jsexp) ?  substr($value, strlen($this->jsexp))  : '\''.$value.'\'';
                }
                else if(is_bool($value))
                {
                    $output .= ($value ? 'true' : 'false');
                }
                else
                {
                    $output .= $value;
                }

                $output .= ',';
            }
        }

        $output = rtrim($output, ',');
        return $enc_left.$output.$enc_right;
    }

    /**
     * JS expression
     * 
     * Prefixes a string with the JS expression flag
     * Strings with this flag will not be quoted by encode() so they are evaluated as expressions
     * 
     * @param   string  $str
     * @return  string
     */
    function js($str)
    {
        return $this->jsexp.$str;
    }
}

这篇关于防止使用PHP json_encode()引用某些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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