我在尝试将json字符串反序列化为.NET视图模型类时遇到错误。错误是“输入字符串格式不正确”。这是我用来反序列化的代码:
string stats = "{\"Total\":1059,\"Completed\":87,\"TotalUnique\":238,\"CompletedUnique\":51,\"AverageTime\":\"21:28:03.3070000\",\"Results\":[[{\"Image\":{\"Id\":\"514d7259-9b25-48cb-ac92-b4641afbcb14\",\"Title\":\"Hildagarde Schadenfreude \",\"Url\":\"http://eentertainmentcms.blob.core.windows.net/content/deddfa80-7aa0-4fcb-bfdd-39e74040864f\",\"ImageAlt\":\"Image 3 Alt\",\"ImageCredit\":\"Image 3 Credit\",\"PercentChosen\":0.0},\"Count\":5},{\"Image\":{\"Id\":\"37dff9f1-8390-4e63-b67a-436e7f2748b2\",\"Title\":\"Mary Louise Parker\",\"Url\":\"http://eentertainmentcms.blob.core.windows.net/content/c9570b24-1c0c-45a3-a1ce-43369e7f3a9f\",\"ImageAlt\":\"Image 4 Alt\",\"ImageCredit\":\"Image 4 Credit\",\"PercentChosen\":0.0},\"Count\":6},{\"Image\":{\"Id\":\"421431b4-d4ff-46ae-9241-df2dc4062c05\",\"Title\":\"Sarah Jessica Parker\",\"Url\":\"http://eentertainmentcms.blob.core.windows.net/content/975b3bf4-d17a-4548-8167-59415d51e0c6\",\"ImageAlt\":\"Image 5 Alt\",\"ImageCredit\":\"Image 5 Credit\",\"PercentChosen\":0.0},\"Count\":8}],[{\"Image\":{\"Id\":\"81459ca5-d76d-4655-b620-b30b7a980b10\",\"Title\":\"Claire Danes\",\"Url\":\"http://eentertainmentcms.blob.core.windows.net/content/712dd313-a08f-472a-a8e4-50cc1c150b2a\",\"ImageAlt\":\"Image 3 Alt\",\"ImageCredit\":\"Image 3 Credit\",\"PercentChosen\":0.0},\"Count\":7},{\"Image\":{\"Id\":\"de7b9131-4b0d-4c07-8f6e-b6fe3c34eef4\",\"Title\":\"Lea Michelle\",\"Url\":\"http://eentertainmentcms.blob.core.windows.net/content/bc20ffee-f41d-450d-bf64-f4e754a465c1\",\"ImageAlt\":\"Image 4 Alt\",\"ImageCredit\":\"Image 4 Credit\",\"PercentChosen\":0.0},\"Count\":12}]]}"
JavaScriptSerializer JSS = new JavaScriptSerializer();
ComparisonGameResults model = JSS.Deserialize<ComparisonGameResults>(stats);
这是我的ViewModel:
public class ComparisonGameResults
{
public int Total { get; set; }
public int Completed { get; set; }
public int TotalUnique { get; set; }
public int CompletedUnique { get; set; }
public string AverageTime { get; set; }
public Result[][] Results { get; set; }
}
public class Result
{
public ResultImage Image { get; set; }
public int Count { get; set; }
}
public class ResultImage
{
public string Id { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public string ImageAlt { get; set; }
public string ImageCredit { get; set; }
public int PercentChosen { get; set; }
}
我使用与字符串中的内容完全相同的结构,基于有效的JSON创建了View Model类。有什么建议吗?
答案 0 :(得分:0)
PercentChosen
看起来像JSON(0.0)中的浮点值,但在视图模型中声明为int
。
尝试将PercentChosen
声明为decimal
或float
。