[英]My .NET API returns JSON as a String. How do I make it return?
I have an SQL Server proc that returns JSON. 我有一个返回JSON的SQL Server proc。 I know the proc is working and returning valid JSON, as I can test it with various tools including the SQL function isjson. 我知道proc正在工作并返回有效的JSON,因为我可以使用各种工具(包括SQL函数isjson)对其进行测试。
However, some tools when calling my API that calls these procs, are unable to verify the JSON as it's getting wrapped in double quotes and escaping every double quote in the JSON. 但是,某些调用我的API的工具调用这些proc时,无法验证JSON,因为它被包裹在双引号中并转义了JSON中的每个双引号。
This is the SQL output: 这是SQL输出:
{"APIResult":[{"ID":200,"Status_Message":"Success","Developer_Message":"Successful login for D56D10AC-3A74-42FC-8BB5-F783A6C0C556 33E4F907-F1E5-4F1B-8CA5-5521291E683A (AppID: (null)).","User_Message":"Successful login","User":[{"ID":"D56D10AC-3A74-42FC-8BB5-F783A6C0C556"}]}]}
And this is the output from postman: 这是邮递员的输出:
"{\"APIResult\":[{\"ID\":200,\"Status_Message\":\"Success\",\"Developer_Message\":\"Successful login for D56D10AC-3A74-42FC-8BB5-F783A6C0C556 33E4F907-F1E5-4F1B-8CA5-5521291E683A (AppID: (null)).\",\"User_Message\":\"Successful login\",\"User\":[{\"ID\":\"D56D10AC-3A74-42FC-8BB5-F783A6C0C556\"}]}]}"
Some parsing tools (jsonlint) are fine with the second format and return it as valid JSON. 某些解析工具(jsonlint)适用于第二种格式,并将其作为有效JSON返回。 Others ( http://json.parser.online.fr/ ), not so much. 其他( http://json.parser.online.fr/ ),不是很多。 Postman is obviously happy, but my mobile developer is unable to get it to work as valid JSON. 邮递员显然很高兴,但是我的移动开发人员无法使它作为有效的JSON工作。
After some help here, I understand that the API "should" be outputting a header content-type of application/JSON and the output should not be a string. 经过一番帮助之后,我了解到API“应该”正在输出应用程序/ JSON的标头内容类型,并且输出不应为字符串。
The controller has: 控制器具有:
public string Get(string key) {
string message = "";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd2);
da.Fill(ds);
message = ds.Tables[0].Rows[0][0].ToString();
return message;
}
Which kind of implies it is being converted to a string. 哪种暗示将其转换为字符串。
So my question is: 所以我的问题是:
How do a set the header on the output, making it return JSON not a string? 如何在输出上设置标头,使其返回JSON而不是字符串?
The data is most likely being double serialized. 数据很可能会被双重序列化。 This the escape characters in the actual response. 这是转义符在实际中的反应。
since the data from the database is already a JSON string then return the content as is, but the actions would need to be refactored a bit. 由于来自数据库的数据已经是JSON字符串,因此按原样返回内容,但是需要对操作进行一些重构。
public IHttpActionResult Get(string key) {
//...
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd2);
da.Fill(ds);
var json = ds.Tables[0].Rows[0][0].ToString();
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(json, Encoding.UTF8, "application/json");
return ResponseMessage(response);
}
The raw JSON is passed as StringContent
directly to the response to avoid the framework trying to serialize it for you. 原始JSON作为StringContent
直接传递给响应,以避免框架尝试为您序列化它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.