解决json object转string,value值存在特殊符号,无法解析问题

  • Post author:
  • Post category:其他


昨天在JSON.stringify()转数组的时候,发现一直报错,最终确定原因为string中的空格在html显示的时候,会自动加上

 


知道了问题所在,下面讲解如何解决问题。我们在取数据时,用HTMLDecode2()方法过滤下特殊字符即可

function HTMLDecode2(str)
{
    if (str.length === 0)
        return "";

    var result = "" + str;
    result = result.replace(">", ">");
    result = result.replace("&lt;", "<");
    result = result.replace("&nbsp;", " ");
    result = result.replace("&quot;", "\"");
    result = result.replace("&#39;", "\'");
    //对斜线的转义  
    result = result.replace("\\\\", "\\");
    //注意php中替换的时候只能用双引号"\n"
    result = result.replace("\\n", "\n");  
    result = result.replace("\\r", "\r");

    return result;
}



版权声明:本文为quwenzhe原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。