-1

This is my JSON

{
   "Id":535325,
   "Etiqueta":"RENOV_CORPORATIVOS_MAI23",
   "Urgente":false,
   "DataCriacao":"2023-05-01T00:00:00",
   "Entidade":{
      "Id":2551,
      "RazaoSocial":"IBR",
      "NomeInterno":"IBR",
      "Cnpj":"23.000.000\/0001-06",
      "Vinculo":"Fornecedor"
   },
   "Empresa":{
      "Id":20193,
      "RazaoSocial":"AND",
      "NomeFantasia":"SCH",
      "Cnpj":"08.000.000\/0001-42",
      "CapitalSocial":300.00,
      "Municipio":"SAO PAULO",
      "UF":"SP"
   },
   "ItensFila":[
      {
         "key":"FGTS",
         "value":{
            "Id":10133534,
            "IdDocumento":3,
            "Positiva":false,
            "Nomenclatura":"FGTS"
         }
      },
      {
         "key":"MPF",
         "value":{
            "Id":10133566,
            "IdDocumento":502,
            "Positiva":false,
            "Nomenclatura":"MPF"
         }
      },
      {
         "key":"CADIN_SP_SaoPaulo",
         "value":{
            "Id":10133552,
            "IdDocumento":391,
            "Positiva":true,
            "Nomenclatura":"CADIN_SP_SaoPaulo"
         }
      },
      {
         "key":"RFB",
         "value":{
            "Id":10133533,
            "IdDocumento":1,
            "Positiva":true,
            "Nomenclatura":"RFB",
            "Detalhamento":{
               "Id":19464811,
               "OcorrenciaCurta":"RFB\/PGFN",
               "p0":"RFB\/PGFN"
            }
         }
      }
   ]
}

I need to deserialize it to a complex object, as follows:

public class Fila
{
    public int Id { get; set; }
    public string Etiqueta { get; set; }
    public bool Urgente { get; set; }
    public int IdSolicitacao { get; set; }
    public DateTime DataCriacao { get; set; }
    public Entidade Entidade { get; set; }
    public Empresa Empresa { get; set; }
    public Dictionary<string, ItemFila> ItensFila { get; set; }
    public string Risco { get; set; }
}
public class ItemFila
{
    public string Nomenclatura { get; set; }
    public int Id { get; set; }
    public int IdDocumento { get; set; }
    public bool Positiva { get; set; }
    
    public Detalhamento? Detalhamento { get; set; }
}
public class Detalhamento
{
    public int Id { get; set; }
    public string OcorrenciaCurta { get; set; }
    public string OcorrenciaLonga { get; set; }
    public string p0 { get; set; }
}

note the Dictionary<string, ItemFila> in Fila Class.

I could not parse json to fill this dictionary. I'm receiving an error saying:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Ibr.Api.Risco.Models.ItemFila]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

how can I deserialize it?

Andy Schmitt
  • 441
  • 1
  • 6
  • 23
  • 1
    Before thinking about deserialization, did you try serializing these objects to JSON? – Ozan BAYRAM May 29 '23 at 10:51
  • `ItensFila` is defined as a `Dictionary` while it's an array in you json. You can use `List` or `ItenFila[]` or whatever equivalent type instead. – Amir Popovich May 29 '23 at 10:52
  • @AmirPopovich the aim is to have a Dictionary with a string key and an object as value. For example ItensFila['RFB'] will have the ItemFila RFB inside. – Andy Schmitt May 29 '23 at 10:56
  • 1
    @AndySchmitt - Then your json needs to change to `{"RFB":{"Id":10133533,"IdDocumento":1,"Positiva":true,"Nomenclatura":"RFB","Detalhamento":{"Id":19464811,"OcorrenciaCurta":"RFB\/PGFN","p0":"RFB\/PGFN"}}}` or start using converts in order to support a non trivial serialization. – Amir Popovich May 29 '23 at 10:59
  • Internet search: newtonsoft json deserialize dictionary from key value array... And you will find duplicated question – Selvin May 29 '23 at 11:00
  • Does this answer your question? [Deserialize json to list of KeyValue pairs](https://stackoverflow.com/questions/50279506/deserialize-json-to-list-of-keyvalue-pairs) – Charlieface May 29 '23 at 11:23
  • Use a `List>` https://dotnetfiddle.net/R31VOq – Charlieface May 29 '23 at 11:23

1 Answers1

-1

"the aim is to have a Dictionary with a string key and an object as value". If you want a dictionary, you can use a JsonConstructor to convert a list to a dictionary

public class Fila
{
    //... another properties
    
    public Dictionary<string, ItemFila> ItensFila { get; set; }
    
    [JsonConstructor]
    public Fila(JArray ItensFila)
    {
        this.ItensFila=ItensFila.ToDictionary(i => (string)i["key"], 
                                              i => i["value"].ToObject<ItemFila>());
    }
    public Fila() { }
}
Serge
  • 40,935
  • 4
  • 18
  • 45