JSONResult to String

Dustin Laine picture Dustin Laine · Dec 31, 2010 · Viewed 107.3k times · Source

I have a JsonResult that is working fine, and returning JSON from some POCO's. I want to save the JSON as a string in a DB.

public JsonResult GetJSON()
{
    JsonResult json = new JsonResult
    {
        Data = GetSomPocos()
    }; 
    return json;
}

I need to audit the response, so I want to save the json into a DB. I am having trouble finding a way to get the JSON as a string.

Any help is appreciated.

Answer

SLaks picture SLaks · Dec 31, 2010

You're looking for the JavaScriptSerializer class, which is used internally by JsonResult:

string json = new JavaScriptSerializer().Serialize(jsonResult.Data);