Search code examples
c#unit-testingobjecthttpcontext

How to properly extract content from object converted to string c#?


I am writing Unit-Tests and am trying to extract a value from a HttpResponseMessage. I am trying to get the value resultCount. Currenly my string looks like this:"{"resultCount":5,"works":null,"success::false,"errors"null}"

Any ideas how I can get to the 'resultCount:5'


Solution

  • Add a reference to your project to "System.Web.Extensions.dll". Then Try:

    var jss = new JavaScriptSerializer();
    var dict = jss.Deserialize<Dictionary<string, string>>("{'resultCount':5,'works':null,'success':false,'errors' : null}");
    
    Console.WriteLine(dict["resultCount"]);
    

    You will need use using System.Web.Script.Serialization;