简体   繁体   English

使用 .NET Core 3.1 中的 System.Text.Json 反序列化会导致 null 属性值

[英]Deserialization with System.Text.Json in .NET Core 3.1 results in null property values

I have the following json:我有以下 json:

{
 "GameConfig":{
  "TeamConstraint":{
     "SameTeamPlayers":[
        "Raghav",
        "Surya"
     ],
     "OppositeTeamPlayers":[
        "Wolfman",
        "Pawan"
     ]
  },
  "Players":[
     "Tramp",
     "Surya",
     "Raghav",
     "Krishna",
     "Sanjay",
     "Bala",
     "Wolfman",
     "Eagle",
     "Sai",
     "Pawan",
     "Joo",
     "Srikanth"
  ],
  "Ranks":{
     "Tramp":10,
     "Surya":10,
     "Raghav":8,
     "Krishna":8,
     "Eagle":8,
     "Sai":8,
     "Sanjay":7.5,
     "Pawan":5,
     "Wolfman":5.0,
     "Srikanth":6,
     "Bala":4.5,
     "Joo":1.5
   }
  }
}

I am writing a simple console app .NET Core 3.1.我正在编写一个简单的控制台应用程序 .NET Core 3.1。 I have tried both the JSON.NET as well as the new MSFT System.Text.Json deserializer.我已经尝试过 JSON.NET 以及新的 MSFT System.Text.Json 解串器。 But I get only null values for all of the properties with the following Model object.但是我只得到所有属性的 null 值,下面是 Model object。

[Serializable]
public class GameConfig
{
    public TeamConstraint TeamConstraint { get; set; }
    
    public List<string> Players { get; set; }
    
    public Dictionary<string, double> Ranks { get; set; }
}

[Serializable]
public class TeamConstraint
{
    public List<string> OppositeTeamPlayers { get; set; }

    public List<string> SameTeamPlayers { get; set; }
}

I have tried several additional options like using a contract resolver with CamelCase properties and changing the code and json accordingly but nothing has worked so far.我已经尝试了几个其他选项,例如使用具有 CamelCase 属性的合同解析器并相应地更改代码和 json,但到目前为止没有任何效果。 What am I missing?我错过了什么?

This is the code I am using to deserialize.这是我用来反序列化的代码。

var jsonString = File.ReadAllText(TeamGeneratorStandardSettings.Default.GameConfigFile);
var gameConfig = JsonSerializer.Deserialize<GameConfig>(jsonString);

You are trying to deserialize into GameConfig , whereas that is simply a top-level property of your overall json object structure.您正在尝试反序列化为GameConfig ,而这只是整个 json object 结构的顶级属性。 You need a new top-level type to deserialize into that contains your GameConfig object.您需要一个新的顶级类型来反序列化,其中包含您的GameConfig object。

public class GameData
{
    public GameConfig GameConfig { get; set; } 
}

var jsonString = File.ReadAllText(TeamGeneratorStandardSettings.Default.GameConfigFile);
var gameConfig = JsonSerializer.Deserialize<GameData>(jsonString);

Alternatively, you could remove the wrapping object from your json and still deserialize directly to a GameConfig object:或者,您可以从 json 中删除包装 object 并仍然直接反序列GameConfig object:

{
  "TeamConstraint":{
     "SameTeamPlayers":[
        "Raghav",
        "Surya"
     ],
     "OppositeTeamPlayers":[
        "Wolfman",
        "Pawan"
     ]
  },
  "Players":[
     "Tramp",
     "Surya",
     "Raghav",
     "Krishna",
     "Sanjay",
     "Bala",
     "Wolfman",
     "Eagle",
     "Sai",
     "Pawan",
     "Joo",
     "Srikanth"
  ],
  "Ranks":{
     "Tramp":10,
     "Surya":10,
     "Raghav":8,
     "Krishna":8,
     "Eagle":8,
     "Sai":8,
     "Sanjay":7.5,
     "Pawan":5,
     "Wolfman":5.0,
     "Srikanth":6,
     "Bala":4.5,
     "Joo":1.5
   }
  }
var jsonString = File.ReadAllText(TeamGeneratorStandardSettings.Default.GameConfigFile);
var gameConfig = JsonSerializer.Deserialize<GameConfig>(jsonString);

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

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