حدد ديناميكيًا معلومات JSON المطلوبة
-
20-12-2019 - |
سؤال
أحتاج إلى إلغاء تسلسل بعض JSON ولكن المشكلة هي أن جميع كائنات JSON الخاصة بي ليس لها نفس التنسيق تمامًا.فيما يلي مثال على JSON الذي يجب علي إلغاء تسلسله:
[
{
"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",
}
]
},
}
]
كما ترون، تظهر بعض الحقول لبعض الكائنات فقط، على سبيل المثال "التكريم_الشخصي".أحتاج إلى إلغاء تسلسل JSON في هذه الفئة:
public class PlayerData
{
public string Name { get; set; }
public string BallonDor {get; set; }
public string Information1{ get; set; }
public string Country{ get; set; }
}
أستخدم هذه الطريقة الطويلة حقًا وتحظر تطبيقي:(في هذا المثال أستخدم Json الذي يأتي من ملف نصي ولكن عادةً ما يتعين علي تقديم طلب 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
});
}
}
}
تعمل هذه الطريقة ولكنها ليست فعالة وتمنع واجهة المستخدم الخاصة بي.كيف يمكنني إنشاء كائن "PlayerData" جديد دون وجود كل عبارات "else if" هذه؟
شكرًا لك !
ملاحظة :السؤال يختلف عن هذا السؤال تصفية معلومات JSon
يحرر :
إليك كيف حصلت على 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
});
}
المحلول
لقد حصلت على استثناء لأن بعض البيانات لا تملك Personal_Honours
الملكية على سبيل المثال.ثم حاولت الوصول Ballon_Dor
الممتلكات من أ null
المرجع الذي يؤدي إلى الاستثناء.ستعمل هذه الطريقة مع نموذج JSON الذي نشرته:
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
});
}
...ولكن النهج أعلاه لا يزال عرضة للخطأ اعتمادًا على مدى اتساق سلسلة JSON لدينا.ربما يكون النهج الأكثر قوة هو الحصول على فئات نموذجية لتعيين سلسلة JSON كما اقترح @L.B في التعليق.