Cha*_*Des 0 c# asp.net-mvc json jsonresult
我有一个非常复杂的字符串代表一个json对象.我需要将其转换为某种形式,以便我可以在MVC控制器中将其作为JsonResult返回.
string result = "[
{
""TagGroupName"": ""group1"",
""Tags"": [
{
""TagName"": ""G1tag1""
},
{
""TagName"": ""G1tag2""
},
{
""TagName"": ""G1tag3""
}
]
},
{
""TagGroupName"": ""group2"",
""Tags"": [
{
""TagName"": ""G2tag1""
},
{
""TagName"": ""G2tag2""
}
]
}
]";
Run Code Online (Sandbox Code Playgroud)
该字符串是动态构建的.
不确定我是否在正确的轨道,但我使用NewtonSoft将其解析为"JObject",但我还需要将此字符串转换为JsonResult类型(应该被Jquery识别为Json对象)
由于您已经有了一个字符串,因此不需要返回JsonResult.JsonResult基本上将对象转换为JSON.
您可以在ContentResult中返回它并指定正确的内容类型:
string result = @"[{ ""TagGroupName"": ""group1"", ""Tags"": [{""TagName"":""G1tag1""},{""TagName"":""G1tag2""},{""TagName"":""G1tag3""}]}, { ""TagGroupName"": ""group2"", ""Tags"": [{""TagName"":""G2tag1""},{""TagName"":""G2tag2""}]}]";
return new ContentResult { Content = result, ContentType = "application/json" };
Run Code Online (Sandbox Code Playgroud)
只需将Action方法ActionResult作为返回类型,这应该可行
| 归档时间: |
|
| 查看次数: |
1078 次 |
| 最近记录: |