How do I handle newlines in JSON?

后端 未结 10 1315
后悔当初
后悔当初 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: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']);
    

提交回复
热议问题