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

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
月光博客
月光博客
爱范儿
爱范儿
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
GbyAI
GbyAI
腾讯CDC
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
U
Unit 42
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
L
LangChain Blog

博客园 - 举重-若轻

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