【问题标题】:Returning Json results without property names返回没有属性名称的 Json 结果
【发布时间】:2012-03-18 11:11:55
【问题描述】:

很可能是一个相当琐碎的问题,但我根本找不到合适的答案。我想返回一个“JsonResult”,而实际结果没有任何属性名称。这是我想要实现的一个小例子:

["xbox", 
["Xbox 360", "Xbox cheats", "Xbox 360 games"], 
["The official Xbox website from Microsoft", "Codes and walkthroughs", "Games and accessories"],
["http://www.xbox.com","http://www.example.com/xboxcheatcodes.aspx", "http://www.example.com/games"]]

是的,一些非常普通的源代码已经存在,但我怀疑这是否相关。但是,这里是:

public JsonResult OpensearchJson(string search)
{
    /* returns some domain specific IEnumerable<> of a certain class */
    var entites = DoSomeSearching(search); 

    var names = entities.Select(m => new { m.Name });
    var description = entities.Select(m => new { m.Description });
    var urls = entities.Select(m => new { m.Url });
    var entitiesJson = new { search, names, description, urls };
    return Json(entitiesJson, JsonRequestBehavior.AllowGet);
}

【问题讨论】:

    标签: .net json asp.net-mvc-3 opensearch


    【解决方案1】:

    给你:

    public ActionResult OpensearchJson(string search)
    {
        /* returns some domain specific IEnumerable<> of a certain class */
        var entities = DoSomeSearching(search); 
    
        var names = entities.Select(m => m.Name);
        var description = entities.Select(m => m.Description);
        var urls = entities.Select(m => m.Url);
        var entitiesJson = new object[] { search, names, description, urls };
        return Json(entitiesJson, JsonRequestBehavior.AllowGet);
    }
    

    更新:

    对于那些急于在没有实际存储库的情况下进行测试的人:

    public ActionResult OpensearchJson(string search)
    {
        var entities = new[]
        {
            new { Name = "Xbox 360", Description = "The official Xbox website from Microsoft", Url = "http://www.xbox.com" },
            new { Name = "Xbox cheats", Description = "Codes and walkthroughs", Url = "http://www.example.com/xboxcheatcodes.aspx" },
            new { Name = "Xbox 360 games", Description = "Games and accessories", Url = "http://www.example.com/games" },
        };
    
        var names = entities.Select(m => m.Name);
        var description = entities.Select(m => m.Description);
        var urls = entities.Select(m => m.Url);
        var entitiesJson = new object[] { search, names, description, urls };
        return Json(entitiesJson, JsonRequestBehavior.AllowGet);
    }
    

    返回:

    [
        "xbox",
        [
            "Xbox 360",
            "Xbox cheats",
            "Xbox 360 games"
        ],
        [
            "The official Xbox website from Microsoft",
            "Codes and walkthroughs",
            "Games and accessories"
        ],
        [
            "http://www.xbox.com",
            "http://www.example.com/xboxcheatcodes.aspx",
            "http://www.example.com/games"
        ]
    ]
    

    这正是预期的 JSON。

    【讨论】:

    • @gdoron,因为在提供的模型类型上 Json 方法 reflects 使用的 JavaScripSerializer。在询问之前尝试代码。我的更新可能会帮助您更好地理解。
    • 嗨达林。这么晚才回复很抱歉。这正是我一直在寻找的。我在我的环境中对其进行了测试,它就像一个魅力。我将把这个问题多留几分钟——也许其他人想出了另一个聪明的解决方案——而不是关闭它。我真的很感谢你的努力。谢谢。
    猜你喜欢
    • 2016-11-08
    • 2013-12-16
    • 2013-09-05
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多