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

推荐订阅源

Recent Announcements
Recent Announcements
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
量子位
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
博客园 - Franky
M
MIT News - Artificial intelligence
U
Unit 42
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
J
Java Code Geeks
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
T
The Blog of Author Tim Ferriss
V
V2EX

博客园 - 举重-若轻

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