asp.net mvc json.net 响应

发布于 2024-10-18 02:27:52 字数 2380 浏览 2 评论 0原文

我第一次为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2024-10-25 02:27:52

以下是我在 MVC/Ajaxy 应用程序中所做的事情。定义一个新类:

public class JsonResultData
{
    private bool _success = true;
    public bool Success
    {
        get { return _success; }
        set { _success = value; }
    }

    public object Value { get; set; }
    public List<string> Errors { get; set; }

    public JsonResultData()
    {
        this.Errors = new List<string>();
    }
}

然后将返回数据值设置为:

jsonNetResult.Data = new JsonResultData { Value = results };

JsonResultData.Success 属性是您要首先在 json 响应中测试的属性。它默认为 true,但如果服务器端方法出现问题,您可以将其设置为 false 并添加错误消息,如下所示:

    catch (Exception ex)
    {
        List<string> errors = new List<string>();
        errors.Add(ex.ToString());
        jsonNetResult.Data = new JsonResultData { Success = false, Errors = errors };
    }

Here's what I do in my MVC/Ajaxy apps. Define a new class:

public class JsonResultData
{
    private bool _success = true;
    public bool Success
    {
        get { return _success; }
        set { _success = value; }
    }

    public object Value { get; set; }
    public List<string> Errors { get; set; }

    public JsonResultData()
    {
        this.Errors = new List<string>();
    }
}

Then set your return data value to:

jsonNetResult.Data = new JsonResultData { Value = results };

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:

    catch (Exception ex)
    {
        List<string> errors = new List<string>();
        errors.Add(ex.ToString());
        jsonNetResult.Data = new JsonResultData { Success = false, Errors = errors };
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文