【问题标题】:Problem serializing Json a List Object in C#在 C# 中序列化 Json 列表对象的问题
【发布时间】:2021-07-28 07:54:56
【问题描述】:

我有一个来自自定义对象的结果列表。我尝试以各种方式对其进行序列化,但是它总是返回带有这些“/:”符号的数据。

我在传统的 ASPX 应用程序中使用 .NET Framework 4.7,这里是返回错误序列化数据的函数代码:

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);
            }
}

我尝试用几个库对其进行序列化,它总是返回带有这些符号的数据

数据示例:

"[{\"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\"}]"

请知道发生了什么?谢谢

【问题讨论】:

  • 我猜你的意思是\"而不是/:,对吧?您如何获得上述价值?您是否将其写入文件并查看文件?您在调试器中查看它吗?如果是调试器,那么您的问题很可能与this one 重复。
  • 我的意思是,我需要在异步调用中读取它,这样它就不会让我通过它,因为它不能很好地形成对象列表。我需要你给我一个清晰的 Json 格式的结构,以便从 AJAX 中使用。
  • 等等...这是在 ASP.NET 项目中吗?您可能正在对其进行双重序列化。如果是这种情况,您的问题可能与this one 重复。
  • 是的,它是一个带有 ASPX 的 .Net 项目。在 .Net core 和 MVC 中我没有遇到任何问题。
  • 底部的字符串来自调试器。如果将其保存到文件中,它将看起来很正常。将其复制并粘贴到this website 并单击“PHP JSON 解码”,它将显示文件中的内容。

标签: c# asp.net json .net serialization


【解决方案1】:

尝试使用此方法序列化 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);
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多