复杂的数据模型和 json.net 丝化问题



我有以下数据模型

class Tournament{
int Id { get; set;}
ICollection<League> Leagues { get; set;}
}
class League{
int Id {get;set;};
Tournament Parent { get; set;}
ICollection<Player> Players { get; set;}
ICollection<Round>  { get; set;}
}
class Player{
int Id  { get; set;}
League Parent  { get; set;}
ICollection<Game> Games { get; set;}
}
class Round{
int Id  { get; set;}
ICollection<Game> Games { get; set;}
League Parent  { get; set;}
}
class Game{
int Id { get; set;}
Player playerOne { get; set;}
Player playerTwo  { get; set;}
Round Parent { get; set;}
}

我在使用 newtsoft 进行序列化和反序列化时遇到问题 json.net。

我收到以下文件 https://api.myjson.com/bins/1aa4kl

使用以下 JSON 网络设置

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Local,
TypeNameHandling = TypeNameHandling.Objects,
Formatting = Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
};

但是正如您从输出文件中看到的那样,这是不正确的,我希望游戏存储对玩家和游戏的引用,但它没有正确引用它们。除了联赛到锦标赛之外,没有映射任何父子关系。

我没有任何类的数据属性。

谁能就出了什么问题提供建议?是否需要更改数据模型?

谢谢

我设法得到了一个工作模型:

public class JsonRoot
{
[JsonProperty("$id")]
public string Id_X { get; set; }
[JsonProperty("$type")]
public string Type_X { get; set; }
public bool UploadedToDatabase { get; set; }
public string Id { get; set; }
public bool TournamentStarted { get; set; }
public int MaximumNumberOfRounds { get; set; }
public string Name { get; set; }
public League[] Leagues { get; set; }
public object SaveFilePath { get; set; }
public object SavedFileName { get; set; }
public object DefaultPairingMethod { get; set; }
public int DefaultPairingLag { get; set; }
public bool IsTournamentRanked { get; set; }
public string UserId { get; set; }
public object StreamUrl { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public int NumberOfPlacesForPrizes { get; set; }
public bool isTournamentDirector { get; set; }
}

这里需要[JsonProperty("$id")],因为属性名称中不允许$


public class League
{
[JsonProperty("$id")]
public string ID_X { get; set; }
[JsonProperty("$type")]
public string Type_X { get; set; }
public bool UploadedToDatabase { get; set; }
public string Id { get; set; }
public object Name { get; set; }
public int NumberOfRounds { get; set; }
public object Parent { get; set; }
public Player[] Players { get; set; }
public Round[] Rounds { get; set; }
public bool LeagueStarted { get; set; }
public object Prizes { get; set; }
}

public class Round
{
[JsonProperty("$id")]
public string ID_X { get; set; }
[JsonProperty("$type")]
public string Type_X { get; set; }
public bool UploadedToDatabase { get; set; }
public string Id { get; set; }
public object Parent { get; set; }
public List<Game> Games { get; set; }
public bool HasStarted { get; set; }
public string TimeStarted { get; set; }
public int RoundNumber { get; set; }
}

public class Game
{
[JsonProperty("$id")]
public string ID_X { get; set; }
[JsonProperty("$type")]
public string Type_X { get; set; }
public object Parent { get; set; }
public bool UploadedToDatabase { get; set; }
public string Id { get; set; }
public int TableNumber { get; set; }
public string PlayerOneId { get; set; }
public string PlayerTwoId { get; set; }
public object PlayerOne { get; set; }
public object PlayerTwo { get; set; }
public int PlayerOneScore { get; set; }
public int PlayerTwoScore { get; set; }
public object HighestScoringWord { get; set; }
public string ScoresSubmittedDateTime { get; set; }
}

public class Player
{
[JsonProperty("$id")]
public string ID_X { get; set; }
[JsonProperty("$type")]
public string Type_X { get; set; }
public bool UploadedToDatabase { get; set; }
public string Id { get; set; }
public object Parent { get; set; }
public List<Game> Games { get; set; }
public int PlayerId { get; set; }
public string FirstName { get; set; }
public string Lastname { get; set; }
public object Username { get; set; }
public object Email { get; set; }
public double Rating { get; set; }
public int PointsFor { get; set; }
public int PointsAgainst { get; set; }
public double NumberOfWins { get; set; }
public double NumberOfDraws { get; set; }
public double NumberOfLosses { get; set; }
public int Spread { get; set; }
public string FullName { get; set; }
public bool IsSelected { get; set; }      
}

我试图像这样解决"父"问题,但它没有序列化:

public class Reference
{
[JsonProperty("$ref")]
public string ref_id { get; set; }
}

但我最终改用了object...

我相信这可以改进,但它应该告诉你要走:D

你可以用Guid和东西代替string Id...

对于调试,我建议在类中添加一些override ToString()

像这样测试:

var test = JsonConvert.DeserializeObject<JsonRoot>(json);

相关内容

  • 没有找到相关文章

最新更新