本文介绍了动态过滤JSON信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好, 我需要反序列化一些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"," C ountry":" Sweeden"," Other_Informations":{" Information1":[ {" name":" example",} ] },} ] 如您所见,某些字段仅针对某些对象显示,例如"Personal_Honours"。 我需要将JSON反序列化到这个类中: public class PlayerData { public string Name {get;组; } public string BallonDor {get;组; } public string Information1 {get;组; } 公共字符串CurrentClub {get;组; } } 我使用的方法非常长并阻止了我的应用: (在这个例子中,我使用Json来自文本文件,但通常 我必须发出REST请求..) StreamReader reader = File.OpenText(" TextFile1.txt"); 列表< PlayerData> DataList控件; 动态值= JsonConvert .DeserializeObject< dynamic>(reader.ReadToEnd()); DataList = new List< PlayerData>(); foreach(动态数据价值) { 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" 对象没有所有这些 '否则如果' 陈述? 谢谢!解决方案 首先你的JSON不正确并且要将新对象添加到列表中你必须只做 class 程序 { static void Main ( string [] args ) { List < 您的班级名称 > sampleList = new 列表 < your class name >(); sampleList 。 添加 ( new 您的类名 () { abc = " Main Street" , xyz = " 1234" }); } } public class " your class" { public string abc { get ; set ; } public string xyz { get ; set ; } } 如需进一步的帮助,请查看我的文章 http: //social.technet.microsoft.com/wiki/contents/articles/25648.learn-json-parsing-and-handling-live-data-in-10-mins.aspx Hello,I need to deserialize some JSON but the problem is that all of my JSON objects don't have the exact same format.Here is an example of the JSON that I have to deserialize :[ { "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", } ] }, } ]As you can see, some fields appear only for some objects for example "Personal_Honours". I need to deserialize the JSON into this class : public class PlayerData{ public string Name { get; set; } public string BallonDor {get; set; } public string Information1{ get; set; } public string CurrentClub { get; set; }}I use this method which is really long and blocks my app : (In this exmple I use Json that comes frome a textfile but usually I have to make a REST request..)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 }); } } }This method is working but it is not efficient and blocks my UI. How can I do to create a new "PlayerData" object without having all of those 'else if' statements ? Thank you ! 解决方案 Firstly your JSON is not correct and to add new object to list you have to do just class Program{ static void Main(string[] args) { List<your class name> sampleList = new List<your class name>(); sampleList.Add(new your class name() { abc = "Main Street", xyz = "1234" }); }}public class "your class"{ public string abc{ get; set; } public string xyz{ get; set; }}For further help please have a look at my article http://social.technet.microsoft.com/wiki/contents/articles/25648.learn-json-parsing-and-handling-live-data-in-10-mins.aspx 这篇关于动态过滤JSON信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 22:38