Zend_JSON:Encode messing up - why?

前端 未结 3 1999
囚心锁ツ
囚心锁ツ 2021-02-13 02:50

My Zend_Json is messing up in encoding an object here. I\'m encoding an associative array which has two elements: Element one is another associative array while element 2 is an

相关标签:
3条回答
  • 2021-02-13 03:31

    This usually happens if you use substr() or use $somestring[0] on a utf-8 string which has multibyte letters. Use mb_ prefixed functions to edit this sort of data, then you will not have problem with json_encode()

    0 讨论(0)
  • 2021-02-13 03:33

    I see only one possible area where NULL may be returned from Encoder.php. I'd begin debugging by doing some var_dumps inside this function in Zend/Json/Encoder.php

    protected function _encodeDatum(&$value)
    {
        $result = 'null';
    
        if (is_int($value) || is_float($value)) {
            $result = (string) $value;
            $result = str_replace(",", ".", $result);
        } elseif (is_string($value)) {
            $result = $this->_encodeString($value);
        } elseif (is_bool($value)) {
            $result = $value ? 'true' : 'false';
        }
    
        return $result;
    }
    

    I'm not quite sure why you're HTML string would not be recognized as such, but I might try typecasting it prior to encoding.

    array('html' => (string) $yourHtmlStr);
    
    0 讨论(0)
  • 2021-02-13 03:37

    You fix this by updating your PHP version to a version later than version 5.2.7.

    There was a serious UTF-8 bug in PHP's json_encode function before that version. See the changelog for more details.


    Since you note that you are using PHP version 5.2.9.2 your version should be good :) Have you tried routing the specific contents of the html through PHP's json_encode() manually?

    Or maybe through utf8_encode() or utf8_decode()?

    0 讨论(0)
提交回复
热议问题