2013-04-27 74 views
0

我在嘗試將json字符串反序列化爲.NET視圖模型類時出現錯誤。錯誤是「輸入字符串格式不正確」。下面是我使用的反序列化的代碼:將JSON字符串反序列化爲.NET ViewModel

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視圖模型類字符串。有什麼建議?

+0

結果看起來像'[['嵌套數組。 – vishakvkt 2013-04-27 00:30:40

回答

0

PercentChosen看起來像JSON(0.0)中的浮點值,但在視圖模型中聲明爲int

嘗試聲明PercentChosendecimalfloat