0

I have a list of results that come from a custom object. I have tried to serialize it in various ways, however it always returns the data with these "/:" signs.

I am using .NET Framework 4.7 in a traditional ASPX app, here is the code of the function that returns the mis-serialized data:

public static string loadAsyncFotografiasTags()
{
            try
            {
                NoticiaLandingNegocio obrNoticiaPortal = new NoticiaLandingNegocio();
                List<ENFotografiaSearchTag> objNoticiaPortal = obrNoticiaPortal._LandingSearchFotografiabyTags("COVID19, PANDEMIA");

                JsonSerializer ser = new JsonSerializer();
                string jsonresp = JsonConvert.SerializeObject(objNoticiaPortal);

                return jsonresp;
            }
            catch (Exception Err)
            {
                throw new Exception(Err.Message);
            }
}

I have tried to serialize it with several libraries and it always returns the data with those signs

Data example:

"[{\"idFotografia\":96223,\"Leyenda\":\"El Ministro de Salud, Oscar Ugarte, participó en el taller de comunicación de riesgo: \\\"La experiencia de la preparación contra la pandemia de influenza\\\". Foto: ANDINA / Rubén Grández.\",\"Descripcion\":\"\",\"Fecha\":\"2009-05-28T00:00:00\",\"Imagen\":\"000096223M.jpg\",\"Seccion\":\"Política\",\"Fotografo\":\"ANDINA/archivo\",\"URLPhoto\":\"http://andina.pe/agencia/foto-el-ministro-salud-oscar-ugarte-participo-el-taller-comunicacion-riesgo-experiencia-de-preparacion-contra-pandemia-influenza-foto-andina-ruben-grandez-96223.aspx\",\"URLImageFotografia\":\"https://portal.andina.pe/EDPfotografia2/Thumbnail/2009/05/28/000096223M.jpg\"},{\"idFotografia\":96226,\"Leyenda\":\"El Ministro de Salud, Oscar Ugarte,declara luego de participar en el taller de comunicación de riesgo: \\\"La experiencia de la preparación contra la pandemia de influenza\\\". \\r\\nFoto: ANDINA / Rubén Grández.\",\"Descripcion\":\"\",\"Fecha\":\"2009-05-28T00:00:00\",\"Imagen\":\"000096226M.jpg\",\"Seccion\":\"Política\",\"Fotografo\":\"ANDINA/archivo\",\"URLPhoto\":\"http://andina.pe/agencia/foto-el-ministro-salud-oscar-ugartedeclara-luego-participar-el-taller-comunicacion-riesgo-experiencia-de-preparacion-contra-pandemia-influenza-\\r\\nfoto-andina-ruben-grandez-96226.aspx\",\"URLImageFotografia\":\"https://portal.andina.pe/EDPfotografia2/Thumbnail/2009/05/28/000096226M.jpg\"}]"

Please any idea what is happening? Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I assume you mean `\"` rather than `/:`, right? How are you getting the value above? Are you writing it to a file and looking at the file? Are you looking at it in the debugger? If it's the debugger, then your question is likely a duplicate of [this one](https://stackoverflow.com/questions/18759324/can-the-visual-studio-debugger-display-strings-unquoted-unescaped). – ProgrammingLlama May 06 '21 at 01:22
  • I mean, I need to read it in an asynchronous call, and that way it doesn't let me go through it because it doesn't form a list of objects well. I need you to give me a clean structure in Json format to be consumed from AJAX. – Daniel Lazarte May 06 '21 at 01:30
  • Wait... is this in an ASP.NET project? You're probably double-serializing it. If that's the case, your question is probably a duplicate of [this one](https://stackoverflow.com/questions/7070759/double-quotes-in-returned-json). – ProgrammingLlama May 06 '21 at 01:30
  • yes, it is a .Net project with ASPX. IN .Net core and MVC I have had no problems. – Daniel Lazarte May 06 '21 at 01:33
  • 1
    That string at the bottom is from the debugger. If you save it to a file it will look normal. Copy and paste that in to [this website](https://codebeautify.org/json-decode-online) and click "PHP JSON decode" and it will show you what will end up in the file. – Andy May 06 '21 at 01:48

1 Answers1

0

Try to use this method for serializing JSON.

    public static void JsonSerialize(List<string> obj, string fileName)
    {
        using (var sw = new System.IO.StreamWriter($"{fileName}.json"))
        {
            using (var jw = new Newtonsoft.Json.JsonTextWriter(sw))
            {
                var serializer = new Newtonsoft.Json.JsonSerializer();
                jw.Formatting = Newtonsoft.Json.Formatting.Indented;
                serializer.Serialize(jw, obj);
            }
        }
    }
turaltahmazli
  • 333
  • 2
  • 7