简体   繁体   English

动态选择所需的JSON信息

[英]Dynamically Select Required JSON information

I need to deserialize some JSON but the problem is that all of my JSON objects don't have the exact same format. 我需要反序列化一些JSON,但是问题是我所有的JSON对象都没有完全相同的格式。 Here is an example of the JSON that I have to deserialize : 这是我必须反序列化的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",
        }
      ]
    },
  }
 ]

As you can see, some fields appear only for some objects for example "Personal_Honours". 如您所见,某些字段仅对某些对象显示,例如“ Personal_Honours”。 I need to deserialize the JSON into this class : 我需要将JSON反序列化到此类中:

public class PlayerData
{
    public string Name { get; set; }
    public string BallonDor {get; set; }
    public string Information1{ get; set; }
    public string Country{ 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..) 我使用这种方法的时间很长,并且阻塞了我的应用程序:(在此示例中,我使用来自文本文件的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

                                });

                            }

                        }

                    }

This method is working but it is not efficient and blocks my UI. 此方法有效,但效率不高,并阻止了我的UI。 How can I do to create a new "PlayerData" object without having all of those 'else if' statements ? 如何在没有所有“ else if”语句的情况下创建新的“ PlayerData”对象?
Thank you ! 谢谢 !

PS : The question is different from this one Filter JSon Information PS:问题不同于这一过滤器JSon信息

EDIT : 编辑:

Here is how I got the RunTimeBinderExcepetion : 这是我得到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

        });
    }

You got exception because some data doesn't have Personal_Honours property for example. 您之所以例外,是因为某些数据没有Personal_Honours属性。 Then you tried to access Ballon_Dor property from a null reference which trigger the exception. 然后,您尝试从引发异常的null引用访问Ballon_Dor属性。 This way will work for sample JSON you posted : 这种方式适用于您发布的示例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
    });
}

...but above approach is still error prone depending on how consistent is the JSON string we have. ...但是上述方法仍然容易出错,具体取决于我们拥有的JSON字符串的一致性。 More robust approach is maybe to have model classes to map JSON string as suggested by @LB in comment. 更强大的方法可能是让模型类映射@LB在注释中建议的JSON字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM