Question

Je dois désérialiser du JSON mais le problème est que tous mes objets JSON n'ont pas exactement le même format.Voici un exemple du JSON que je dois désérialiser :

[
  {
    "Player_Name": "Zlatan Ibrahimovic",
    "Country": "Sweeden",
    "Other_Informations": {
      "Information1": [

      ]
    },
  },
   {
    "Player_Name": "Pavel Nedved",
    "Country": "Czech Republic",
    "Personal_Honours": 
        {
            "Ballon_DOr": "One",
        },
    "Other_Informations": {
      "Information1": [

      ]
    },
  },
   {
    "Player_Name": "Zinedine Zidane",
    "Country": "Sweeden",
    "Other_Informations": {
      "Information1": [
        {
          "name": "example",
        }
      ]
    },
  }
 ]

Comme vous pouvez le constater, certains champs n'apparaissent que pour certains objets, par exemple "Personal_Honours".Je dois désérialiser le JSON dans cette classe :

public class PlayerData
{
    public string Name { get; set; }
    public string BallonDor {get; set; }
    public string Information1{ get; set; }
    public string Country{ get; set; }
}

J'utilise cette méthode qui est vraiment longue et bloque mon application :(Dans cet exemple, j'utilise Json qui provient d'un fichier texte mais je dois généralement faire une requête REST..)

StreamReader reader = File.OpenText("TextFile1.txt");
List<PlayerData> DataList;
dynamic value= JsonConvert
    .DeserializeObject<dynamic>(reader.ReadToEnd());


DataList = new List<PlayerData>();
    foreach (dynamic data in value)
                    {

                        if (data.Personal_Honours == null)
                        {
                            if (data.Other_Informations.Information1 == null)
                            {
                                DataList.Add(new PlayerData
                                {
                                    Name = data.Player_Name,
                                    Country = data.Country,
                                });
                            }
                            else                            
                            {
                                DataList.Add(new PlayerData
                                {
                                    Name = data.Player_Name,
                                    Country = data.Country,
                                    Information1 = data.Informations.Information1
                                });

                            }

                        }
                        else 
                        {
                            if (data.Other_Informations.Information1 == null)
                            {

                                DataList.Add(new PlayerData
                                {
                                    Name = data.Player_Name,
                                    Country = data.Country,
                                    BallonDor = data.Personal_Honours.Ballon_DOr

                                });
                            }
                            else 
                            {
                                DataList.Add(new PlayerData
                                {
                                    Name = data.Player_Name,
                                    Country = data.Country,
                                    BallonDor = data.Personal_Honours.Ballon_DOr,
                                    Information1 = data.Informations.Information1

                                });

                            }

                        }

                    }

Cette méthode fonctionne mais elle n'est pas efficace et bloque mon interface utilisateur.Comment puis-je créer un nouvel objet "PlayerData" sans avoir toutes ces instructions "else if" ?
Merci !

P.S :La question est différente de celle-ci Filtrer les informations JSon

MODIFIER :

Voici comment j'ai obtenu le RunTimeBinderExcepetion :

 List<PlayerData> datalist = new List<PlayerData>();

    foreach (dynamic pl in timeline)
    {



        datalist.Add(new PlayerData 
        { 

         Name  = pl.Player_Name , 
         Country = pl.Country ,
          BallonDor = pl.Personal_Honours.Ballon_Dor,
           Information1 = pl.Other_Informations.Information1.name

        });
    }
Était-ce utile?

La solution

Vous avez une exception car certaines données n'ont pas Personal_Honours propriété par exemple.Ensuite, vous avez essayé d'accéder Ballon_Dor propriété d'un null référence qui déclenche l’exception.Cette méthode fonctionnera pour l'exemple JSON que vous avez publié :

List<PlayerData> datalist = new List<PlayerData>();
foreach (dynamic pl in timeline)
{
    //object initialization is done later just for the sake of easier debugging
    //...so we can spot unexpected data which cause exception easily
    var Name = pl.Player_Name;
    var Country = pl.Country;
    var BallonDor = pl.Personal_Honours != null ? pl.Personal_Honours.Ballon_Dor : null;
    var Information1 = pl.Other_Informations.Information1.Count > 0 ? 
                                pl.Other_Informations.Information1[0].name : 
                                null;
    datalist.Add(new PlayerData 
    { 
        Name  = Name , 
        Country = Country ,
        BallonDor = BallonDor,
        Information1 = Information1
    });
}

... mais l'approche ci-dessus est toujours sujette aux erreurs en fonction de la cohérence de la chaîne JSON dont nous disposons.Une approche plus robuste consiste peut-être à disposer de classes de modèles pour mapper la chaîne JSON, comme suggéré par @L.B dans le commentaire.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top