JSONResult to String

不打扰是莪最后的温柔 提交于 2019-12-17 10:25:35

问题


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.


回答1:


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

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



回答2:


You can also use Json.NET.

return JsonConvert.SerializeObject(jsonResult.Data);



回答3:


json = " { \"success\" : false, \"errors\": { \"text\" : \"绑定登录失败!\" } }";            
return new MemoryStream(Encoding.UTF8.GetBytes(json));


来源:https://stackoverflow.com/questions/4571985/jsonresult-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!