惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
The Cloudflare Blog
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
F
Fortinet All Blogs
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
爱范儿
爱范儿
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
小众软件
小众软件
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队

博客园 - 举重-若轻

Survey 2遍的解决办法 Error occurred in deployment step 'Activate Features': Feature with Id 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' is not installed in this farm using powershell to change the some sub-webs' system master page AssetUrlSelector export to excel 用powershell更新user profile service中的一个字段 create a 全局临时表 Generate a random integrater Remote Debugging GAC'd Assemblies in SharePoint get all user profiles redirecting to custom user profile page css的em概念 异步调用的一个例子 CSS备忘 SharePoint 资料 Server.MapPath方法的应用方法 JSON的客户端和服务器操作 jquery 参考材料 备忘 用powershell更新user的display name
.NET种Json时对单引号和特殊字符串的处理
举重-若轻 · 2012-09-27 · via 博客园 - 举重-若轻

在Asp.NET的Json处理中,通常会遇到使用单引号或其他特殊字符,如果直接使用,会截断Json数据,造成JS错误无法正确运行。

其实只要使用Javascript的escape方法编码字符串后就可以解决这个问题。

但是在C#代码中怎么解决这个问题呢?

我们自然就想到了使用Server.UrlEncode方法来编码数据,可是实践后发现编码后的有些字符在JS中无法被unescape方法还原。

其实解决方法很简单,引用Microsoft.JScript程序集后,使用GlobalObject类的escape方法就行了。

这个方法作用和Server.UrlEncode类似,但是它相当于是在C#代码里调用了javascript的escape方法,自然而然可以保证能被unescape方法还原。

.cs中

1 Microsoft.JScript.GlobalObject.escape(strJS)

.js中

var csStr = unescape(json.csStr)

要先添加引用
添加引用--".NET"选项卡---"Microsoft.JScript"和"Microsoft.Vsa"加入之后

才能去使用using

另: 

可以 用replace解决里面包含单引号的问题?

Quote from:http://www.jiaonan.tv/html/blog/1/28008.htm