gpt4 book ai didi

c# - 如何从 C# 方法返回 Json 对象

转载 作者:可可西里 更新时间:2023-11-01 08:02:36 45 4
gpt4 key购买 nike

我正在尝试修复需要 Json 响应的 ASP.NET WebAPI 方法。然而,它返回的是一个字符串。

最初它是 returing XML 格式,但我已将此行添加到 App_Start\WebApiConfig.cs 中的 mvc 代码,以便默认返回 Json。

config.Formatters.Remove(config.Formatters.XmlFormatter);

我们已经更新了 c# 方法以使用 NewtonSoft:

public string Get()
{
string userid = UrlUtil.getParam(this, "userid", "");
string pwd = UrlUtil.getParam(this, "pwd", "");
string resp = DynAggrClientAPI.openSession(userid, pwd);

JsonSerializer ser = new JsonSerializer();
string jsonresp = JsonConvert.SerializeObject(resp);

return resp;
}

resp var 以字符串类型返回:

"{status:\"SUCCESS\",data:[\"4eb97d2c6729df98206cf214874ac1757649839fe4e24c51d21d\"]}"

jsonresp var 看起来像这样:

"\"{status:\\\"SUCCESS\\\",data:[\\\"4eb97d2c6729df98206cf214874ac1757649839fe4e24c51d21d\\\"]}\""

在 Chrome 的 F12 开发工具中,数据对象是:

""{status:\"SUCCESS\",data:[\"4eb97d2c6729df98206cf214874ac1757649839fe4e24c51d21d\"]}""

在控制台工具中,angular.fromJson(data) 的结果:

"{status:"SUCCESS",data:["4eb97d2c6729df98206cf214874ac1757649839fe4e24c51d21d"]}"

关于如何正确返回 Json 对象而不是任何字符串类型的建议,我将不胜感激。


更新

通过拦截resp var,并使用下面朱先生的建议,我可以在客户端成功地实现一个漂亮干净的Json对象。关键是 resp 需要在两个键:值对周围包含双引号:

public HttpResponseMessage Get()
{
string userid = UrlUtil.getParam(this, "userid", "");
string pwd = UrlUtil.getParam(this, "pwd", "");
string resp = DynAggrClientAPI.openSession(userid, pwd);

resp = "{\"status\":\"SUCCESS\",\"data\":[\"194f66366a6dee8738428bf1d730691a9babb77920ec9dfa06cf\"]}"; // TEST !!!!!

var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(resp, System.Text.Encoding.UTF8, "application/json");
return response;
}

在 Chrome 控制台中,响应是:

Object {status: "SUCCESS", data: Array[1]}
data: Array[1]
status: "SUCCESS"
__proto__: Object

最佳答案

resp 已经是一个 JSON 字符串,但它不是有效的 JSON(键没有用引号引起来("))。如果返回到 angular,则JavaScript JSON.parse() 方法无法对其进行反序列化。但是,您可以使用 JSON.NET 将其反序列化为 JObject,然后再次将其序列化为有效的 JSON 并创建您自己的 HttpResponseMessage...

public HttpResponseMessage Get()
{
string userid = UrlUtil.getParam(this, "userid", "");
string pwd = UrlUtil.getParam(this, "pwd", "" );

string resp = DynAggrClientAPI.openSession(userid, pwd);
var jObject = JObject.Parse(resp);

var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(jObject.ToString(), Encoding.UTF8, "application/json");
return response;
}

或者您可以只返回 JObject 并让 Web API 为您序列化它...

public JObject Get()
{
string userid = UrlUtil.getParam(this, "userid", "");
string pwd = UrlUtil.getParam(this, "pwd", "" );

string resp = DynAggrClientAPI.openSession(userid, pwd);
var jObject = JObject.Parse(resp);

return jObject;
}

无论哪种情况,Web API 调用都应返回此 JSON,现在有效...

{
"status": "SUCCESS",
"data": [
"4eb97d2c6729df98206cf214874ac1757649839fe4e24c51d21d"
]
}

在角度代码中,您必须挖掘出存储在名为 data 的数组中的 session ID...

userService.openUserSession(rzEnvJson).then(function (response) {
var sessionResponse = response.data; // or simply response, depending if this is a promise returned from $http
$rootScope.rgSessionVars.sessionID = sessionResponse.data[0];
});

关于c# - 如何从 C# 方法返回 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25145609/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com