简体   繁体   English

我的.NET API以字符串形式返回JSON。 我该如何退货?

[英]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.

相关问题 从字符串中提取GUID。 我该怎么做? - Extract a GUID from a string. How can I do it? 我如何使这个字符串成为 JSON 对象? - How do i make this string a JSON object? 如何使aspx Web方法将列表作为JSON对象返回? - How do I make my aspx web method return the list as a JSON object? 我如何获取我的Json的参数并在发布Post Int Web API 2 .net之前进行验证? - How i can get the arguments of my Json and validate before of make a post int web api 2 .net? 如何 map 对象列表返回我的 ASP.NET API 应用程序中的特定属性? - How do I map a list of objects to return a specific property in my ASP.NET API application? 如何使Json.NET成为默认的Json序列化程序 - How Do I Make Json.NET the Default Json Serializer 如何创建一个在C ++中返回字符串数组的方法? - How do I make a method that returns a string array in C++? 我希望将NumericUpDown绑定到一个值,并且还包含一个数字字符串。 我怎么做? - I want a NumericUpDown to bind to a value and also contain a numeric string. How do I do that? 当我在查询中使用“包含”时,ASP.NET Core Web API 返回不完整的 JSON 数据 - ASP.NET Core Web API returns incomplete JSON data when I use 'include' in my query C# .NET Core Web API,抛出 AuthorizationException,客户端得到 500。如何让它返回 401? - C# .NET Core Web API, throwing an AuthorizationException, client gets a 500. How do I make it return a 401 instead?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM