JSON中单引号的处理

在Json字符串中如果有单引号,用eval转化的时候会报错。解决方法:

1、C#代码

       [DebuggerStepThrough]
        public static string ToJson(this object target)
        {
            var serializer = new JavaScriptSerializer();
            return serializer.Serialize(target);
        }

2、js代码

    js中使用JSON.parse(jsonstr)把json字符串转化为对象,使用此方法时,属性名必须用双引号括起来,他比eval的效率高。但是此方法在IE8以下有问题,这时需要引入json2.js,下载地址:https://github.com/douglascrockford/JSON-js

原文地址:https://www.cnblogs.com/liuhaitao/p/2998525.html