简体   繁体   中英

Deserializing JSON string into .NET ViewModel

I am getting an error while trying to deserialize a json string into a .NET view model class. The error is "Input string was not in a correct format." Here is the code I am using to deserialize:

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);

And here is my 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; }
}

I created the View Model classes based on the valid JSON using the same exact structure as what is in the string. Any advice?

PercentChosen looks like a floating point value in the JSON (0.0) but is declared as an int in the View Model.

Try declaring PercentChosen as a decimal or float .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM