js 引号 转义字符 json字符串

2年前 (2022) 程序员胖胖胖虎阿
115 0 0
element_obj.NewTitle.value = json_obj.NewTitle.replace(/\"/g, "\"");
model.NewTitle = model.NewTitle.Replace("\"", """);
            model.NewContent = model.NewContent.Replace("\"", """);

对象  转换为 json字符串时候 , 数据的引号要提前替换为转移字符"或指定字符#, 前台 转json对象时 不报错; 转对象后要替换会来

 

二 发现   Newtonsoft.Json.JsonConvert 都不会报错

var z = get_dictdata(list);
var t = Newtonsoft.Json.JsonConvert.SerializeObject(list);

对比了下输出字符串

又问了下群里的 羽毛哥哥, 怎么替换字符转义字符的

public static string Replace_Json_char(this string str)
        {
            //var 他 = string.Empty.Replace("\"", "\\\\\""); //   双 "替换为\"
            //他 = string.Empty.Replace("\\", "\\\\"); //  单 \ 替换为 \\
            string[] oldValues = { "\\", "\"" };
            string[] newValues = { "\\\\", "\\\"" };
            for (int i = 0; i < oldValues.Length; i++)
            {
                str = str.Replace(oldValues[i], newValues[i]);
            }
            return str;
        }

 

 

版权声明:程序员胖胖胖虎阿 发表于 2022年9月2日 下午3:08。
转载请注明:js 引号 转义字符 json字符串 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...