Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
Escaping quotes in Newtonsoft JSON

I've an object:

public class Test 
{
    public string Prop1 { get; set; }
}

I'd like to serialize it to json in a view, so in my cshtml:

<script type="text/javascript">
   var myJson = JSON.parse('@Html.Raw(JsonConvert.Serialize(Model.MyTest))');
</script>

It works, until Prop1 contains quotes, because it gets rendered as:

var myJson = JSON.parse('{"Prop1":"\"Quoted text\""}');

Unfortunately, such a line throws parse error. I know that it should be:

 var myJson = JSON.parse('{"Prop1":"\\"Quoted text\\""}');

How can I configure Newtonsoft to serialize it in a proper way?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You should not parse the string for a second time, since already serialized it as JSON, you can directly use it in Javascript (the JS in JSON).

var myJson = @Html.Raw(JsonConvert.Serialize(Model.MyTest));

Will output:

var myJson = {"Prop1":"\"Quoted text\""};

And, because you always need a JSFiddle to prove it works.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda