我需要反序列化一些 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",
        }
      ]
    },
  }
 ]

如您所见,某些字段仅针对某些对象显示,例如“Personal_Honours”。我需要将 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

                                });

                            }

                        }

                    }

此方法有效,但效率不高,并且会阻塞我的 UI。如何在没有所有这些“else if”语句的情况下创建新的“PlayerData”对象?
谢谢 !

附:这个问题和这个不一样 过滤 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 在评论中所建议的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top