2010-11-11 100 views
2

我使用MVC控制器返回JSON:C#MVC逃生JSON

public JsonResult Json() 
     { 
      return Json(MyJsonString); 
     } 

我想JSON来进行转义。有没有人知道确切的方法来完成这个? 感谢

EDIT1:JSON被发送Flash视频播放器

--MB

+1

这取决于你想要做什么。你的意思是你希望它被转义,这样你就可以在明文上看到它(用于调试或相似)? – 2010-11-11 14:42:40

回答

1

我以在此基础上您的样品和评论的刺,但MyJsonString一个字符串,它已经是JSON编码?听起来是这样,因此它被双重编码。

如果是这样,那么你可能有更好的运气回来,你用来创建JSON编码字符串,如对象:

public virtual ActionResult Json() 
    { 
     var someObject = MethodThatCreatesAnObject(); 
     //or just create it on the fly 
     //var someObject = new { val1 = "value", val2 = "another" }; 
     return Json(someObject); 
    } 

或者,您可以创建一个具有字符串作为其模式类型的控制和不具有任何内容,除了<%: Model %>,然后你可以这样做:

public virtual ActionResult Json() 
    { 
     return ActionResult("MyJsonControl", MyJsonString); 
    } 

但我真的考虑前者之前与后者去。