Visualizzazione dei risultati da 1 a 10 su 10

Hybrid View

  1. #1
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,887
    il post e' visibile.


    Semplicemente, il JSON rappresenta un array di persone, ma è strano.
    Stavo usando le classi:

    codice:
        public class containerRamiRete
        {
            public out_ramoRete ramo { get; set; }
        }
    
    
        public class out_ramoRete
        {
            public IList<nodoRete> listaAgenti { get; set; }
        }
    
    
        public class nodoRete
        {
            public string UTERET_profilo_rete_comm { get; set; }
            public string DTAIAR_data_da { get; set; }
            public string DTAFAR_data_a { get; set; }
            public string QALRET_qualifica_rete { get; set; }
            public string CDCLAR_cod_controparte { get; set; }
            public string ndg_cdclar { get; set; }
            public string RASOC_ragione_sociale { get; set; }
            public string FILARE_filiale { get; set; }
            public string CDLIAR_cod_controp_liquid { get; set; }
            public string ndg_cdliar { get; set; }
            public string UTPDAR_utente_padre { get; set; }
            public string ndg_utpdar { get; set; }
            public string GRURET_gruppo_appartenenza { get; set; }
            public string AIREC_abilitato_caricamento { get; set; }
            public string ACFRE_abilitato_caricamento_figli { get; set; }
            public string dataRif { get; set; }
            public string XCDRES_responsabile { get; set; }
            public string listaProdottiAbilit { get; set; } // [CA”,”CB”,”DA”] // prodotti abilitati
            public string obbligoFinanziato { get; set; }
        }

    ma alle istruzioni:

    codice:
      
    
    // PROVA 1 -----------------------------------------------------------------------------------------
    // aaaa contiente un oggetto di tipo <out_ramoRete> = NULL
    
    List<out_ramoRete> nodo1 = JsonConvert.DeserializeObject<List<out_ramoRete>>(dati);
    
    
    
    // PROVA 2 -----------------------------------------------------------------          
    //Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ApiClient2.nodoRete]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
    //To fix this error either change the JSON to a JSON array(e.g. [1, 2, 3]) or change the deserialized type so that it is a normal .NET type(e.g.not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object.JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
    //Path 'IFAGE00201', line 1, position 14.'
    
    
    List<nodoRete> nodo2 = JsonConvert.DeserializeObject<List<nodoRete>>(dati);



    ....Restituiva gli errori che ho riportato commentati sopra l'istruzione.


    Poi, grazie a StackOverflow, ho incontrato: https://json2csharp.com e l'ho fatto funzionare.

    Vedo che il tuo esempio è preso da li' (ero da 3 giorni e sono contemporaneamente arrivato alla soluzione).

    Ho solo alcuni dubbi, poi li chiariro' con la software house che ha sviluppato le API.

    Grazie.





    Ah, un'altra istruzione funzionava, provala:

    codice:
    // restituisce un Newtonsoft.Json.Linq.JObject 
    dynamic ramoreteOut = JsonConvert.DeserializeObject(dati);

    ma non sapevo maneggiare l'oggetto di ritorno (Newtonsoft.Json.Linq.JObject ??)
    Ultima modifica di djciko; 23-09-2020 a 16:35

  2. #2
    Utente di HTML.it L'avatar di U235
    Registrato dal
    Mar 2006
    Messaggi
    1,539
    Quote Originariamente inviata da djciko Visualizza il messaggio
    il post e' visibile.
    Io lo vedevo solo entrando dentro il post, ma non risultava nemmeno come ultimo messaggio, ne nella lista degli ultimi messaggi finché non ho aggiunto il secondo post. Questo sia dopo che l'ho scritto stanotte sia fino a poco fa. Poi non so

    Quote Originariamente inviata da djciko Visualizza il messaggio
    Semplicemente, il JSON rappresenta un array di persone, ma è strano.
    Stavo usando le classi:

    codice:
        public class containerRamiRete
        {
            public out_ramoRete ramo { get; set; }
        }
    
    
        public class out_ramoRete
        {
            public IList<nodoRete> listaAgenti { get; set; }
        }
    
    
        public class nodoRete
        {
            public string UTERET_profilo_rete_comm { get; set; }
            public string DTAIAR_data_da { get; set; }
            public string DTAFAR_data_a { get; set; }
            public string QALRET_qualifica_rete { get; set; }
            public string CDCLAR_cod_controparte { get; set; }
            public string ndg_cdclar { get; set; }
            public string RASOC_ragione_sociale { get; set; }
            public string FILARE_filiale { get; set; }
            public string CDLIAR_cod_controp_liquid { get; set; }
            public string ndg_cdliar { get; set; }
            public string UTPDAR_utente_padre { get; set; }
            public string ndg_utpdar { get; set; }
            public string GRURET_gruppo_appartenenza { get; set; }
            public string AIREC_abilitato_caricamento { get; set; }
            public string ACFRE_abilitato_caricamento_figli { get; set; }
            public string dataRif { get; set; }
            public string XCDRES_responsabile { get; set; }
            public string listaProdottiAbilit { get; set; } // [CA”,”CB”,”DA”] // prodotti abilitati
            public string obbligoFinanziato { get; set; }
        }

    ma alle istruzioni:

    codice:
      
    
    // PROVA 1 -----------------------------------------------------------------------------------------
    // aaaa contiente un oggetto di tipo <out_ramoRete> = NULL
    
    List<out_ramoRete> nodo1 = JsonConvert.DeserializeObject<List<out_ramoRete>>(dati);
    
    
    
    // PROVA 2 -----------------------------------------------------------------          
    //Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ApiClient2.nodoRete]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
    //To fix this error either change the JSON to a JSON array(e.g. [1, 2, 3]) or change the deserialized type so that it is a normal .NET type(e.g.not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object.JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
    //Path 'IFAGE00201', line 1, position 14.'
    
    
    List<nodoRete> nodo2 = JsonConvert.DeserializeObject<List<nodoRete>>(dati);



    ....Restituiva gli errori che ho riportato commentati sopra l'istruzione.
    Ma quel json che hai postato non è strutturato in questo modo...

    Quote Originariamente inviata da djciko Visualizza il messaggio
    Poi, grazie a StackOverflow, ho incontrato: https://json2csharp.com e l'ho fatto funzionare.

    Vedo che il tuo esempio è preso da li' (ero da 3 giorni e sono contemporaneamente arrivato alla soluzione).

    Ho solo alcuni dubbi, poi li chiariro' con la software house che ha sviluppato le API.

    Grazie.
    No, non è un esempio preso da li, ma semplicemente quando vuoi ricreare una classe da un json in maniera abbastanza precisa è sufficiente copiare negli appunti il json e dopo da visualstudio apri il menu modifica e scegli incolla speciale->incolla json come classi.
    Niente di complicato o esterno e sei sicuro che hai le classi corrette per quel json.

    Quote Originariamente inviata da djciko Visualizza il messaggio
    Ah, un'altra istruzione funzionava, provala:

    codice:
    // restituisce un Newtonsoft.Json.Linq.JObject 
    dynamic ramoreteOut = JsonConvert.DeserializeObject(dati);

    ma non sapevo maneggiare l'oggetto di ritorno (Newtonsoft.Json.Linq.JObject ??)
    é un dynamic, quindi semplicemente chiami i suoi membri dopo il punto come un qualsiasi oggetto tenendo presente in compilazione non da errori (e ovviamente visual studio non da suggerimenti), ma se il nome del membro della classe non esiste darà un errore in runtime.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.