How do I handle newlines in JSON?

后端 未结 10 1298
后悔当初
后悔当初 2020-11-22 05:28

I\'ve generated some JSON and I\'m trying to pull it into an object in JavaScript. I keep getting errors. Here\'s what I have:

var data = \'{\"count\" : 1, \         


        
相关标签:
10条回答
  • 2020-11-22 06:10

    According to the specification, http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf:

    A string is a sequence of Unicode code points wrapped with quotation marks (U+0022). All characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark (U+0022), reverse solidus (U+005C), and the control characters U+0000 to U+001F. There are two-character escape sequence representations of some characters.

    So you can't pass 0x0A or 0x0C codes directly. It is forbidden! The specification suggests to use escape sequences for some well-defined codes from U+0000 to U+001F:

    • \f represents the form feed character (U+000C).
    • \n represents the line feed character (U+000A).

    As most of programming languages uses \ for quoting, you should escape the escape syntax (double-escape - once for language/platform, once for JSON itself):

    jsonStr = "{ \"name\": \"Multi\\nline.\" }";
    
    0 讨论(0)
  • 2020-11-22 06:11

    I used this function to strip newline or other characters in data to parse JSON data:

    function normalize_str($str) {
    
        $invalid = array(
            'Š'=>'S', 'š'=>'s',  'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z',
            'Č'=>'C', 'č'=>'c',  'Ć'=>'C',  'ć'=>'c',  'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A',
            'Ä'=>'A', 'Å'=>'A',  'Æ'=>'A',  'Ç'=>'C',  'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E',
            'Ì'=>'I', 'Í'=>'I',  'Î'=>'I',  'Ï'=>'I',  'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
            'Õ'=>'O', 'Ö'=>'O',  'Ø'=>'O',  'Ù'=>'U',  'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y',
            'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a',  'á'=>'a',  'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a',
            'æ'=>'a', 'ç'=>'c',  'è'=>'e',  'é'=>'e',  'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i',
            'î'=>'i', 'ï'=>'i',  'ð'=>'o',  'ñ'=>'n',  'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
            'ö'=>'o', 'ø'=>'o',  'ù'=>'u',  'ú'=>'u',  'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
            'ÿ'=>'y', 'Ŕ'=>'R',  'ŕ'=>'r',
            "`" => "'", "´" => "'",  '"' => ',',  '`' => "'",
            '´' => "'", '"' => '\"', '"' => "\"", '´' => "'",
            "’" => "'",
            "{" => "",
            "~" => "",  "–" => "-",  "'" => "'",  "     " => " ");
    
        $str = str_replace(array_keys($invalid), array_values($invalid), $str);
    
        $remove = array("\n", "\r\n", "\r");
        $str = str_replace($remove, "\\n", trim($str));
    
        //$str = htmlentities($str, ENT_QUOTES);
    
        return htmlspecialchars($str);
    }
    
    echo normalize_str($lst['address']);
    
    0 讨论(0)
  • 2020-11-22 06:13

    You could just escape your string on the server when writing the value of the JSON field and unescape it when retrieving the value in the client browser, for instance.

    The JavaScript implementation of all major browsers have the unescape command.

    Example:

    On the server:

    response.write "{""field1"":""" & escape(RS_Temp("textField")) & """}"
    

    In the browser:

    document.getElementById("text1").value = unescape(jsonObject.field1)
    
    0 讨论(0)
  • 2020-11-22 06:14

    I encountered that problem while making a class in PHP 4 to emulate json_encode (available in PHP 5). Here's what I came up with:

    class jsonResponse {
        var $response;
    
        function jsonResponse() {
            $this->response = array('isOK'=>'KO', 'msg'=>'Undefined');
        }
    
        function set($isOK, $msg) {
            $this->response['isOK'] = ($isOK) ? 'OK' : 'KO';
            $this->response['msg'] = htmlentities($msg);
        }
    
        function setData($data=null) {
            if(!is_null($data))
                $this->response['data'] = $data;
            elseif(isset($this->response['data']))
                unset($this->response['data']);
        }
    
        function send() {
            header('Content-type: application/json');
            echo '{"isOK":"' . $this->response['isOK'] . '","msg":' . $this->parseString($this->response['msg']);
            if(isset($this->response['data']))
                echo ',"data":' . $this->parseData($this->response['data']);
            echo '}';
        }
    
        function parseData($data) {
            if(is_array($data)) {
                $parsed = array();
                foreach ($data as $key=>$value)
                    array_push($parsed, $this->parseString($key) . ':' . $this->parseData($value));
                return '{' . implode(',', $parsed) . '}';
            }
            else
                return $this->parseString($data);
        }
    
        function parseString($string) {
                $string = str_replace("\\", "\\\\", $string);
                $string = str_replace('/', "\\/", $string);
                $string = str_replace('"', "\\".'"', $string);
                $string = str_replace("\b", "\\b", $string);
                $string = str_replace("\t", "\\t", $string);
                $string = str_replace("\n", "\\n", $string);
                $string = str_replace("\f", "\\f", $string);
                $string = str_replace("\r", "\\r", $string);
                $string = str_replace("\u", "\\u", $string);
                return '"'.$string.'"';
        }
    }
    

    I followed the rules mentioned here. I only used what I needed, but I figure that you can adapt it to your needs in the language your are using. The problem in my case wasn't about newlines as I originally thought, but about the / not being escaped. I hope this prevent someone else from the little headache I had figuring out what I did wrong.

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