asp.net mvc json.net 响应
我第一次为我的 ASP.NET MVC2 站点实现 Json.NET。
我的原始代码如下所示:
[HttpPost]
public ActionResult FindMe(string searchFirstName, string searchLastName)
{
this.searchFirstName = searchFirstName;
this.searchLastName = searchLastName;
IEnumerable<HomePageUser> results = doSearch();
bool success = (results.Count() == 0) ? false : true;
return Json(new
{
success = success,
results = results
});
}
结果存在问题,因为结果集中的一项是枚举,而我真正想要的是文本值,而不是数字值。此外,日期格式也是一个问题。
所以,然后我找到了 Json.NET 并将我的代码更改为:
[HttpPost]
public JsonNetResult FindMe(string searchFirstName, string searchLastName)
{
this.searchFirstName = searchFirstName;
this.searchLastName = searchLastName;
IEnumerable<HomePageUser> results = doSearch();
bool success = (results.Count() == 0) ? false : true;
JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
jsonNetResult.Data = results;// how to add success true/false info?
return jsonNetResult;
}
这解决了上述两个问题,但现在我想知道如何与我现有的 javascript 代码无缝连接,该代码期望 json 看起来像:
{
"success":true,
"results":[{
"UserId":545,
"FirstName":"Scott",
"LastName":"Roberson"}]
}
这使我能够在继续写出答案之前首先测试response.success,而不是进入处理错误的部分。
所以,我的问题是如何添加一个顶级成功 json 节点以位于结果节点旁边?
谢谢。
更新:
通常情况下,写下问题的行为会在其中一个 Doh! 中激发出一个想法!时刻。
如果我添加:
var returnPackage = new { success = success, results = results};
然后将其添加到 jsonNetResult.Data 中,如下所示:
jsonNetResult.Data = returnPackage;
它工作得很好。
无论如何,谢谢。
最终代码:
[HttpPost]
public JsonNetResult FindMe(string searchFirstName, string searchLastName)
{
this.searchFirstName = searchFirstName;
this.searchLastName = searchLastName;
IEnumerable<HomePageUser> results = doSearch();
bool success = (results.Count() == 0) ? false : true;
var returnPackage = new { success = success, results = results};
JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
jsonNetResult.Data = returnPackage;
return jsonNetResult;
}
I am implementing, for the first time, Json.NET for my ASP.NET MVC2 site.
My original code looked like this:
[HttpPost]
public ActionResult FindMe(string searchFirstName, string searchLastName)
{
this.searchFirstName = searchFirstName;
this.searchLastName = searchLastName;
IEnumerable<HomePageUser> results = doSearch();
bool success = (results.Count() == 0) ? false : true;
return Json(new
{
success = success,
results = results
});
}
The results were problematic due to the fact that one of the items in the results set is an Enum and I really want the Text value, not the numeric one. Additionally, the date format is a problem.
So, I then found Json.NET and changed my code to this:
[HttpPost]
public JsonNetResult FindMe(string searchFirstName, string searchLastName)
{
this.searchFirstName = searchFirstName;
this.searchLastName = searchLastName;
IEnumerable<HomePageUser> results = doSearch();
bool success = (results.Count() == 0) ? false : true;
JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
jsonNetResult.Data = results;// how to add success true/false info?
return jsonNetResult;
}
This fixes the above two issues, but now I'm wondering how to make this seamless with my existing javascript code that was expecting json that looked like:
{
"success":true,
"results":[{
"UserId":545,
"FirstName":"Scott",
"LastName":"Roberson"}]
}
This allowed me to first test for response.success before proceeding to write out the answer, versus moving into the section to handle errors.
So, my question is how do I add a top-level success json node to live beside the results node?
Thanks.
UPDATE:
As is often the case, the act of writing out a question sparked an idea in one of those Doh! moments.
if I add:
var returnPackage = new { success = success, results = results};
then add this to the jsonNetResult.Data like so:
jsonNetResult.Data = returnPackage;
It works perfectly.
Thanks, anyway.
Final code:
[HttpPost]
public JsonNetResult FindMe(string searchFirstName, string searchLastName)
{
this.searchFirstName = searchFirstName;
this.searchLastName = searchLastName;
IEnumerable<HomePageUser> results = doSearch();
bool success = (results.Count() == 0) ? false : true;
var returnPackage = new { success = success, results = results};
JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
jsonNetResult.Data = returnPackage;
return jsonNetResult;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是我在 MVC/Ajaxy 应用程序中所做的事情。定义一个新类:
然后将返回数据值设置为:
JsonResultData.Success 属性是您要首先在 json 响应中测试的属性。它默认为 true,但如果服务器端方法出现问题,您可以将其设置为 false 并添加错误消息,如下所示:
Here's what I do in my MVC/Ajaxy apps. Define a new class:
Then set your return data value to:
JsonResultData.Success property is what you want to test in the json response first. It defaults to true, but if something goes wrong in your server-side method, you set it to false and add your error message, like this: