.net - 返回没有属性名称的 Json 结果

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

很可能是一个相当琐碎的问题,但我就是找不到合适的答案。我想返回一个“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);
}

最佳答案

给你:

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。

关于.net - 返回没有属性名称的 Json 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9491119/

相关文章:

c# - 使用 TreeView 上的 StateImageList 防止颜色渗色

asp.net-mvc - 将电子邮件更改保存在ASP.NET MVC的默认成员资格提供程序中

kendo ui 网格中分页的 Javascript 验证

c# - 元素的 JQuery Mobile 渲染层次结构

ios - 将http请求转成json

java - 安卓无法获取Json

c# - 是否有可能在 .Net C# 中或使用任何 REST API 获取逻辑应用程序工作流操作名称?

.net - ASP.net 报表查看器/报表是否需要 SQL Server 上的 Reporting Services

c# - 从两个列表中计算所有可能的项目对?

c# - 如何反序列化动态Json对象?