Wählen Sie die erforderlichen JSON-Informationen dynamisch aus
-
20-12-2019 - |
Frage
Ich muss etwas JSON deserialisieren, aber das Problem ist, dass nicht alle meine JSON-Objekte genau das gleiche Format haben.Hier ist ein Beispiel für den JSON, den ich deserialisieren muss:
[
{
"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",
}
]
},
}
]
Wie Sie sehen, werden einige Felder nur für einige Objekte angezeigt, beispielsweise „Personal_Honours“.Ich muss den JSON in diese Klasse deserialisieren:
public class PlayerData
{
public string Name { get; set; }
public string BallonDor {get; set; }
public string Information1{ get; set; }
public string Country{ get; set; }
}
Ich verwende diese Methode, die sehr lang ist und meine App blockiert:(In diesem Beispiel verwende ich JSON, das aus einer Textdatei stammt, aber normalerweise muss ich eine REST-Anfrage stellen.)
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
});
}
}
}
Diese Methode funktioniert, ist aber nicht effizient und blockiert meine Benutzeroberfläche.Wie kann ich ein neues „PlayerData“-Objekt erstellen, ohne all diese „else if“-Anweisungen zu haben?
Danke !
P.S.:Die Frage ist eine andere als diese Filtern Sie JSon-Informationen
BEARBEITEN :
So habe ich die RunTimeBinderExcepetion erhalten:
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
});
}
Lösung
Es liegt eine Ausnahme vor, da bei einigen Daten dies nicht der Fall ist Personal_Honours
Immobilien zum Beispiel.Dann haben Sie versucht, darauf zuzugreifen Ballon_Dor
Eigentum von a null
Referenz, die die Ausnahme auslöst.Dieser Weg funktioniert für das von Ihnen gepostete JSON-Beispiel:
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
});
}
... aber der obige Ansatz ist immer noch fehleranfällig, je nachdem, wie konsistent die JSON-Zeichenfolge ist, die wir haben.Ein robusterer Ansatz besteht möglicherweise darin, Modellklassen zur Zuordnung von JSON-Strings zu haben, wie von @L.B im Kommentar vorgeschlagen.