我有以下数据模型
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;}
}
我无法使用json.net newtsoft进行序列化和反序列化。
我收到以下文件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,
};
但是你可以从输出文件中看到它不正确,我希望游戏存储对玩家和游戏的引用,但它没有正确引用它们。除了联赛到锦标赛之外,没有任何父级子关系被映射。
我没有任何类的数据属性。
有人可以告知出了什么问题吗?我需要更改数据模型吗?
由于
答案 0 :(得分:1)
我设法得到了一个工作模型:
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
您可以将string Id
替换为Guid
并填充...
对于debuggin,我建议在类中添加一些override ToString()
。
这样测试:
var test = JsonConvert.DeserializeObject<JsonRoot>(json);