c# - Escaping quotes in Newtonsoft JSON -


i've object:

public class test  {     public string prop1 { get; set; } } 

i'd serialize json in view, in cshtml:

<script type="text/javascript">    var myjson = json.parse('@html.raw(jsonconvert.serialize(model.mytest))'); </script> 

it works, until prop1 contains quotes, because gets rendered as:

var myjson = json.parse('{"prop1":"\"quoted text\""}'); 

unfortunately, such line throws parse error. know should be:

 var myjson = json.parse('{"prop1":"\\"quoted text\\""}'); 

how can configure newtonsoft serialize in proper way?

you should not parse string second time, since serialized json, can directly use in javascript (the js in json).

var myjson = @html.raw(jsonconvert.serialize(model.mytest)); 

will output:

var myjson = {"prop1":"\"quoted text\""}; 

and, because need jsfiddle prove works.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -