C# JsonSerializer.Serialize 在生成的json中添加键值

Posted

技术标签:

【中文标题】C# JsonSerializer.Serialize 在生成的json中添加键值【英文标题】:C# JsonSerializer.Serialize add key value in the generated json 【发布时间】:2021-03-11 17:50:50 【问题描述】:

我需要按 2 个字段对表中的数据进行分组,以便为​​最终用户对可视化进行分类,我尝试这样做,只是生成的 json 不包含作为类别名称的键。

我的桌子:

var niveis = _repository.GetAll();

            var queryNestedGroups =
                    (from con in niveis
                    group con by con.Modulo into newGroup1
                    from newGroup2 in
                        (from con in newGroup1
                         group con by con.Submod)
                    group newGroup2 by newGroup1.Key).ToList();

结果

JsonSerializer.Serialize(queryNestedGroups)

[
  [
    [
      
        "Id": 1,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Produtos",
        "Nome": "Visualizar Produtos",
        "Valor": "CAT_PROD_LISTA"
      ,
      
        "Id": 2,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Produtos",
        "Nome": "Gravar Produtos",
        "Valor": "CAT_PROD_GRAVA"
      ,
      
        "Id": 3,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Produtos",
        "Nome": "Excluir Produtos",
        "Valor": "CAT_PROD_EXCLUI"
      
    ],
    [
      
        "Id": 4,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Grupos",
        "Nome": "Visualizar Grupos",
        "Valor": "CAT_GRUPO_LISTA"
      ,
      
        "Id": 5,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Grupos",
        "Nome": "Gravar Grupos",
        "Valor": "CAT_GRUPO_GRAVA"
      ,
      
        "Id": 6,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Grupos",
        "Nome": "Excluir Grupos",
        "Valor": "CAT_GRUPO_EXCLUI"
      
    ],
    [
      
        "Id": 7,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Categorias",
        "Nome": "Visualizar Catagorias",
        "Valor": "CAT_CAT_LISTA"
      ,
      
        "Id": 8,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Categorias",
        "Nome": "Gravar Categorias",
        "Valor": "CAT_CAT_GRAVA"
      ,
      
        "Id": 9,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Categorias",
        "Nome": "Excluir Categorias",
        "Valor": "CAT_CAT_EXCLUI"
      
    ],
    [
      
        "Id": 10,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Unidades",
        "Nome": "Visualizar Unidades",
        "Valor": "CAT_UNIT_LISTA"
      ,
      
        "Id": 11,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Unidades",
        "Nome": "Gravar Unidades",
        "Valor": "CAT_UNIT_GRAVA"
      ,
      
        "Id": 12,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Unidades",
        "Nome": "Excluir Unidades",
        "Valor": "CAT_UNIT_EXCLUI"
      
    ],
    [
      
        "Id": 13,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Marcas",
        "Nome": "Visualizar Marcas",
        "Valor": "CAT_MARCA_LISTA"
      ,
      
        "Id": 14,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Marcas",
        "Nome": "Gravar Marcas",
        "Valor": "CAT_MARCA_GRAVA"
      ,
      
        "Id": 15,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Marcas",
        "Nome": "Excluir Marcas",
        "Valor": "CAT_MARCA_EXCLUI"
      
    ],
    [
      
        "Id": 16,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Atributos",
        "Nome": "Visualizar Atributos",
        "Valor": "CAT_ATTR_LISTA"
      ,
      
        "Id": 17,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Atributos",
        "Nome": "Gravar Atributos",
        "Valor": "CAT_ATTR_GRAVA"
      ,
      
        "Id": 18,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Atributos",
        "Nome": "Excluir Atributos",
        "Valor": "CAT_ATTR_EXCLUI"
      
    ],
    [
      
        "Id": 19,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Especifica\u00E7\u00F5es",
        "Nome": "Visualizar Especifica\u00E7\u00E3o",
        "Valor": "CAT_SPEC_LISTA"
      ,
      
        "Id": 20,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Especifica\u00E7\u00F5es",
        "Nome": "Gravar Especifica\u00E7\u00E3o",
        "Valor": "CAT_SPEC_GRAVA"
      ,
      
        "Id": 21,
        "IdMod": 1,
        "Modulo": "Catalogo",
        "Submod": "Especifica\u00E7\u00F5es",
        "Nome": "Excluir Especifica\u00E7\u00E3o",
        "Valor": "CAT_SPEC_EXCLUI"
      
    ]
  ],
  [
    [
      
        "Id": 22,
        "IdMod": 2,
        "Modulo": "Cadastros",
        "Submod": "Fornecedores",
        "Nome": "Visualizar Fornecedor",
        "Valor": "CAD_FORN_LISTA"
      ,
      
        "Id": 23,
        "IdMod": 2,
        "Modulo": "Cadastros",
        "Submod": "Fornecedores",
        "Nome": "Gravar Fornecedor",
        "Valor": "CAD_FORN_GRAVA"
      ,
      
        "Id": 24,
        "IdMod": 2,
        "Modulo": "Cadastros",
        "Submod": "Fornecedores",
        "Nome": "Excluir Fornecedor",
        "Valor": "CAD_FORN_EXCLUI"
      
    ],
    [
      
        "Id": 25,
        "IdMod": 2,
        "Modulo": "Cadastros",
        "Submod": "Clientes",
        "Nome": "Visualizar Cliente",
        "Valor": "CAD_CLI_LISTA"
      ,
      
        "Id": 26,
        "IdMod": 2,
        "Modulo": "Cadastros",
        "Submod": "Clientes",
        "Nome": "Gravar Cliente",
        "Valor": "CAD_CLI_GRAVA"
      ,
      
        "Id": 27,
        "IdMod": 2,
        "Modulo": "Cadastros",
        "Submod": "Clientes",
        "Nome": "Excluir Cliente",
        "Valor": "CAD_CLI_EXCLUI"
      
    ]
  ],
  [
    [
      
        "Id": 28,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Bancos",
        "Nome": "Visualizar Bancos",
        "Valor": "CONFIG_BANCOS_LISTA"
      ,
      
        "Id": 29,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Bancos",
        "Nome": "Gravar Bancos",
        "Valor": "CONFIG_BANCOS_GRAVA"
      ,
      
        "Id": 30,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Bancos",
        "Nome": "Excluir Bancos",
        "Valor": "CONFIG_BANCOS_EXCLUI"
      
    ],
    [
      
        "Id": 31,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Estados/Cidade",
        "Nome": "Visualizar Estados/Cidade",
        "Valor": "CONFIG_UFCID_LISTA"
      ,
      
        "Id": 32,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Estados/Cidade",
        "Nome": "Gravar Estados/Cidade",
        "Valor": "CONFIG_UFCID_GRAVA"
      ,
      
        "Id": 33,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Estados/Cidade",
        "Nome": "Excluir Estados/Cidade",
        "Valor": "CONFIG_UFCID_EXCLUI"
      
    ],
    [
      
        "Id": 34,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Condi\u00E7\u00F5es de pagamento",
        "Nome": "Visualizar condi\u00E7\u00E3o de pagamento",
        "Valor": "CONFIG_CONDP_LISTA"
      ,
      
        "Id": 35,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Condi\u00E7\u00F5es de pagamento",
        "Nome": "Gravar condi\u00E7\u00E3o de pagamento",
        "Valor": "CONFIG_CONDP_GRAVA"
      ,
      
        "Id": 36,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Condi\u00E7\u00F5es de pagamento",
        "Nome": "Excluir condi\u00E7\u00E3o de pagamento",
        "Valor": "CONFIG_CONDP_EXCLUI"
      
    ],
    [
      
        "Id": 37,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Tributa\u00E7\u00E3o CFOP",
        "Nome": "Visualizar CFOP",
        "Valor": "CONFIG_TRIBUT_CFOP_LISTA"
      ,
      
        "Id": 38,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Tributa\u00E7\u00E3o CFOP",
        "Nome": "Gravar  CFOP",
        "Valor": "CONFIG_TRIBUT_CFOP_GRAVA"
      ,
      
        "Id": 39,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Tributa\u00E7\u00E3o CFOP",
        "Nome": "Excluir  CFOP",
        "Valor": "CONFIG_TRIBUT_CFOP_EXCLUI"
      
    ],
    [
      
        "Id": 40,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "CST ICMS",
        "Nome": "Visualizar CST ICMS",
        "Valor": "CONFIG_TRIBUT_CSTICMS_LISTA"
      ,
      
        "Id": 41,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "CST ICMS",
        "Nome": "Gravar CST ICMS",
        "Valor": "CONFIG_TRIBUT_CSTICMS_GRAVA"
      ,
      
        "Id": 42,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "CST ICMS",
        "Nome": "Excluir CST ICMS",
        "Valor": "CONFIG_TRIBUT_CSTICMS_EXCLUI"
      
    ],
    [
      
        "Id": 43,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "CST IPI",
        "Nome": "Visualizar CST IPI",
        "Valor": "CONFIG_TRIBUT_CSTIPI_LISTA"
      ,
      
        "Id": 44,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "CST IPI",
        "Nome": "Gravar CST IPI",
        "Valor": "CONFIG_TRIBUT_CSTIPI_GRAVA"
      ,
      
        "Id": 45,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "CST IPI",
        "Nome": "Excluir CST IPI",
        "Valor": "CONFIG_TRIBUT_CSTIPI_EXCLUI"
      
    ],
    [
      
        "Id": 46,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "CST PIS/COFINS",
        "Nome": "Visualizar CST PIS/COFINS",
        "Valor": "CONFIG_TRIBUT_PISCOFINS_LISTA"
      ,
      
        "Id": 47,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "CST PIS/COFINS",
        "Nome": "Gravar CST PIS/COFINS",
        "Valor": "CONFIG_TRIBUT_PISCOFINS_GRAVA"
      ,
      
        "Id": 48,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "CST PIS/COFINS",
        "Nome": "Excluir CST PIS/COFINS",
        "Valor": "CONFIG_TRIBUT_PISCOFINS_EXCLUI"
      
    ],
    [
      
        "Id": 49,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "ICMS UF",
        "Nome": "Visualizar ICMS UF",
        "Valor": "CONFIG_TRIBUT_ICMSUF_LISTA"
      ,
      
        "Id": 50,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "ICMS UF",
        "Nome": "Gravra ICMS UF",
        "Valor": "CONFIG_TRIBUT_ICMSUF_GRAVA"
      ,
      
        "Id": 51,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "ICMS UF",
        "Nome": "Excluir ICMS UF",
        "Valor": "CONFIG_TRIBUT_ICMSUF_EXCLUI"
      
    ],
    [
      
        "Id": 52,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "ORIGENS",
        "Nome": "Visualizar ORIGENS",
        "Valor": "CONFIG_TRIBUT_ORIGENS_LISTA"
      ,
      
        "Id": 53,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "ORIGENS",
        "Nome": "Gravar ORIGENS",
        "Valor": "CONFIG_TRIBUT_ORIGENS_GRAVA"
      ,
      
        "Id": 54,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "ORIGENS",
        "Nome": "Excluir ORIGENS",
        "Valor": "CONFIG_TRIBUT_ORIGENS_EXCLUI"
      
    ],
    [
      
        "Id": 55,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "FORMA PAG.",
        "Nome": "Visualizar FORMA PAG.",
        "Valor": "CONFIG_TRIBUT_FORMAPAG_LISTA"
      ,
      
        "Id": 56,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "FORMA PAG.",
        "Nome": "Gravar FORMA PAG.",
        "Valor": "CONFIG_TRIBUT_FORMAPAG_GRAVA"
      ,
      
        "Id": 57,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "FORMA PAG.",
        "Nome": "Excluir FORMA PAG.",
        "Valor": "CONFIG_TRIBUT_FORMAPAG_EXCLUI"
      
    ],
    [
      
        "Id": 58,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Integra\u00E7\u00E3o Fiscal",
        "Nome": "Visualizar Integra\u00E7\u00E3o Fiscal",
        "Valor": "CONFIG_TRIBUT_INTF_LISTA"
      ,
      
        "Id": 59,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Integra\u00E7\u00E3o Fiscal",
        "Nome": "Gravar Integra\u00E7\u00E3o Fiscal",
        "Valor": "CONFIG_TRIBUT_INTF_GRAVA"
      ,
      
        "Id": 60,
        "IdMod": 3,
        "Modulo": "Configura\u00E7\u00F5es",
        "Submod": "Integra\u00E7\u00E3o Fiscal",
        "Nome": "Excluir Integra\u00E7\u00E3o Fiscal",
        "Valor": "CONFIG_TRIBUT_INTF_EXCLUI"
      
    ]
  ],
  [
    [
      
        "Id": 67,
        "IdMod": 4,
        "Modulo": "Empresa",
        "Submod": "Lojas",
        "Nome": "Visualizar Lojas",
        "Valor": "EMPRESA_LOJAS_LISTA"
      ,
      
        "Id": 68,
        "IdMod": 4,
        "Modulo": "Empresa",
        "Submod": "Lojas",
        "Nome": "Gravar Lojas",
        "Valor": "EMPRESA_LOJAS_GRAVA"
      ,
      
        "Id": 69,
        "IdMod": 4,
        "Modulo": "Empresa",
        "Submod": "Lojas",
        "Nome": "Excluir Lojas",
        "Valor": "EMPRESA_LOJAS_EXCLUI"
      
    ],
    [
      
        "Id": 70,
        "IdMod": 4,
        "Modulo": "Empresa",
        "Submod": "Depositos",
        "Nome": "Visualizar Depositos",
        "Valor": "EMPRESA_DEP_LISTA"
      ,
      
        "Id": 71,
        "IdMod": 4,
        "Modulo": "Empresa",
        "Submod": "Depositos",
        "Nome": "Gravar Depositos",
        "Valor": "EMPRESA_DEP_GRAVA"
      ,
      
        "Id": 72,
        "IdMod": 4,
        "Modulo": "Empresa",
        "Submod": "Depositos",
        "Nome": "Excluir Depositos",
        "Valor": "EMPRESA_DEP_EXCLUI"
      
    ]
  ]
]

不包括键值。 如何在生成的 json 中包含密钥,因为我将在前端的 foreach 中使用它? 谢谢!

【问题讨论】:

能否请您edit 分享minimal reproducible example 的问题?另外,能否请您edit 将您的代码和 JSON 包含为 text 而不是屏幕截图?这里最好不要将图像用于文本数据,请参阅 Discourage screenshots of code and/or errorsWhy not upload images of code on SO when asking a question 了解原因。 请说明您正在使用 which JSON Serializer - NewtonsoftSystem.Text.Json(或其他 -请说明) IGrouping<TKey,TElement> 实现IEnumerable<TElement>System.Text.Json 和 Json.NET 将枚举序列化为数组,而不是对象。有关原因,请参阅 JSON serialize properties on class inheriting list 和 How do I get json.net to serialize members of a class deriving from List<T>。如果您需要解决方法,我们需要查看 minimal reproducible example 并了解您正在使用的序列化程序。 【参考方案1】:

Json.NET 为集合属性 (IEnumerable) 或 Dictionary<TKey, TValue> 序列化 "key": [ array ] 结构,它不知道如何序列化具有属性可枚举元素的类。

您也不能使用[JsonProperty("key")] public IEnumerable<T> Items get; set; ,因为在编译时您不知道"key" 的值。

您可以从您的分组中创建一个讨厌的嵌套字典,并将其序列化:

var queryNestedDictionaries = queryNestedGroups.ToDictionary(g => g.Key, 
    v => v.Values.ToDictionary(gg => gg.Key, gg => gg.Values));

或者为IGrouping<TKey, TValue>创建一个自定义转换器,并将键写入属性名称,将值写入数组。

【讨论】:

以上是关于C# JsonSerializer.Serialize 在生成的json中添加键值的主要内容,如果未能解决你的问题,请参考以下文章

C#进阶C# 泛型

C#进阶C# 匿名方法

C#进阶C# 多线程

C# 教程

[C#教程01]C# 简介

用 C# 编写 C# 编译器,先有鸡还是先有蛋?