0
我有一個字符串:JsonConvert.DeserializeObject轉換ID的方法到零
string model = "{\"Game\":{\"GameId\":1,\"Board\":[0,0,0,0,0,0,0,0,0],\"User1Id\":1,\"User2Id\":2,\"UserIdTurn\":1,\"WinnerId\":0,\"IsGameOver\":false},\"User\":{\"UserId\":1,\"UserName\":\"a\",\"Password\":\"a\",\"Password2\":\"a\",\"Games\":[]}}"
當我使用此代碼:
GameModel myModel = JsonConvert.DeserializeObject<GameModel>(model);
它的工作原理相當不錯,但只有一個問題 - 它將我的遊戲和用戶ID更改爲零。爲什麼會發生,我該如何解決? 我用c#寫的。
這些都是我的課:
public class GameModel
{
public Game Game { get; set; }
public User User { get; set; }
}
public class Game
{
public Game()
{
Board = new SquareState[9];
}
public Game(int user1Id)
{
Board = new SquareState[9];
User1Id = user1Id;
UserIdTurn = user1Id;
}
public int GameId { get; private set; }
public SquareState[] Board { get; set; }
[Required]
public int User1Id { get; set; }
public int User2Id { get; set; }
public int UserIdTurn { get; set; }
public int WinnerId { get; set; }
public bool IsGameOver { get; set; }
}
public class User
{
public User()
{
Games = new List<Game>();
}
public int UserId { get; private set; }
[Required]
[Display(Name = "User Name:")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[Display(Name = "re-enter Password")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Passwords not match")]
public string Password2 { get; set; }
public List<Game> Games { get; set; }
}
能否請您也發佈了'GameModel'類的定義?謝謝 – Christos
json無效。檢查http://jsonlint.com/ – Sajal
我剛剛發佈了我的課程,我正在瀏覽我的json,但到目前爲止不知道需要更改哪些內容。 –