我正在使用JsonConvert.SerializeObject()序列化IEnumerbale对象;它产生带引号的字符串和带空格的转义字符
从Web Api控制器,我使用下面的代码返回该字符串
[HttpGet]
public string GetDegreeCodes(int id)
{
string result = //output from JsonConvert.SerializeObject( );
return result;
}
“ [{\” DegreeId \“:1,\” DegreeName \“:\”高中\“,\” ImageSrc \“:\” http://bootsnipp.com/apple-touch-icon-114x114-pre \ “,\”描述\“:\”获得高中学位\ r \“},{\” DegreeId \“:2,\” DegreeName \“:\” Associate \“,\” ImageSrc \“:\” http ://bootsnipp.com/apple-touch-icon-114x114-pre \“,\”描述\“:\”获取关联度\ r \“},{\” DegreeId \“:3,\” DegreeName \“ :\“单身汉”,\“ ImageSrc \”:\“ http://bootsnipp.com/apple-touch-icon-114x114-pre \”,\“说明\”:\“获得学士学位\ r \” },{\“ DegreeId \”:4,\“ DegreeName \”:\“ Masters \”,\“ImageSrc \“:\” http://bootsnipp.com/apple-touch-icon-114x114-pre \“,\”说明\“:\”获得硕士学位\ r \“},{\” DegreeId \“: 5,\“ DegreeName \”:\“ Doctrate \”,\“ ImageSrc \”:\“http://bootsnipp.com/apple-touch-icon-114x114-pre \“,\”描述\“:\”获得博士学位\“}]”
这是我的ajax,由于多余的包装引号和转义字符,因此无法正确识别JSON,
$.ajax({
url: "/api/helloservice/getdegreecodes",
type: "get",
contentType: "application/text",
data: { id: 1 }
}).done(function (data) {
if (data.length > 0) {
for (i = 0; i < data.length; i++) {
viewEduModel.degreeCodes.push(data[i]);
}
}
});
我需要使用JsonConvert.SerializeObject,因为我使用booksleeve将它作为JSon缓存在我的Redis缓存服务器中,所以我不需要每次都重新序列化并从db读取。如何避免Web API控制器发送引号和反斜杠?我可以简单地返回IEnumerable并让Web Api执行JSOn序列化,但是我需要在redis端进行缓存
您可以像下面这样:
[HttpGet]
public HttpResponseMessage GetDegreeCodes(int id)
{
StringContent sc = new StringContent("Your JSON content from Redis here");
sc.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage resp = new HttpResponseMessage();
resp.Content = sc;
return resp;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句