Search code examples
.netjsonc#-4.0javascriptserializer

JSON string not Deserialize properly with boolean and string


I'm trying to parse a simple JSON response.

The result string is

{"Success":false,"Response":"Error"}

And I've built the class

class JsonResponse
{
  public bool cSuccess { get; set; }
  public string cResponse { get; set; }
}

And parse with the code

JsonResponse message = new JavaScriptSerializer().Deserialize<JsonResponse>(result);

However only message.cSuccess is populated with false, while message.cResponse is still null.

Is there any mistake I've made?


Solution

  • The names of the properties in your class need to match the properties in the JSON string.

    Change the class to be:

    class JsonResponse
    {
      public bool Success { get; set; }
      public string Response { get; set; }
    }