我的.NET API以字符串形式返回JSON.我该如何退货? [英] My .NET API returns JSON as a String. How do I make it return?

查看:51
本文介绍了我的.NET API以字符串形式返回JSON.我该如何退货?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回JSON的SQL Server proc.我知道proc可以正常工作并返回有效的JSON,因为我可以使用各种工具(包括SQL函数isjson)对其进行测试.

I have an SQL Server proc that returns JSON. I know the proc is working and returning valid JSON, as I can test it with various tools including the SQL function isjson.

但是,某些工具在调用调用这些proc的API时,无法验证JSON,因为它被用双引号引起来并转义了JSON中的每个双引号.

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.

这是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"}]}]}

这是邮递员的输出:

"{\"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\"}]}]}"

某些解析工具(jsonlint)适用于第二种格式,并将其作为有效JSON返回.其他的( http://json.parser.online.fr/)不是很多.邮递员显然很高兴,但是我的移动开发人员无法使它作为有效的JSON工作.

Some parsing tools (jsonlint) are fine with the second format and return it as valid JSON. Others (http://json.parser.online.fr/), not so much. Postman is obviously happy, but my mobile developer is unable to get it to work as valid JSON.

在获得一些帮助之后,我了解到API应该"正在输出application/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.

控制器具有:

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.

所以我的问题是:

如何在输出上设置标头,使其返回JSON而不是字符串?

How do a set the header on the output, making it return JSON not a string?

推荐答案

数据很可能会被双重序列化.这是实际响应中的转义字符.

The data is most likely being double serialized. This the escape characters in the actual response.

由于数据库中的数据已经是JSON字符串,因此按原样返回内容,但是需要对操作进行一些重构.

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.

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);
}

原始JSON作为 StringContent 直接传递给响应,以避免框架尝试为您序列化它.

The raw JSON is passed as StringContent directly to the response to avoid the framework trying to serialize it for you.

这篇关于我的.NET API以字符串形式返回JSON.我该如何退货?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆