返回JSON结果没有财产名 [英] Returning Json results without property names

查看:152
本文介绍了返回JSON结果没有财产名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很可能的一个次要的问题,但我根本无法找到一个合适的答案。我想返回JsonResult没有任何有任何财产名实际结果。下面是我想要达到一个小例子:

  [的Xbox,
[的Xbox 360,的Xbox秘籍,Xbox 360游戏],
[官方的Xbox网站从微软,codeS和演练,游戏及配件],
[http://www.xbox.com,http://www.example.com/xboxcheat$c$cs.aspx,http://www.example.com/games]]
 

是的,一些很普通的来源$ C ​​$ C已经存在,但我怀疑这是任何关联。然而,在这里它是:

 公共JsonResult OpensearchJson(字符串搜索)
{
    / *返回一些特定领域的IEnumerable<>某一类* /
    VAR的entites = DoSomeSearching(搜索);

    VAR名称= entities.Select(M =>新建{m.Name});
    Var描述= entities.Select(M =>新建{m.Description});
    VAR的网址= entities.Select(M =>新建{m.Url});
    VAR entitiesJson = {新的搜索,名称,描述,网址};
    返回JSON(entitiesJson,JsonRequestBehavior.AllowGet);
}
 

解决方案

在这里你去:

 公众的ActionResult OpensearchJson(字符串搜索)
{
    / *返回一些特定领域的IEnumerable<>某一类* /
    VAR实体= DoSomeSearching(搜索);

    变种名称= entities.Select(米=> m.Name);
    Var描述= entities.Select(M => m.Description);
    VAR的网址= entities.Select(M => m.Url);
    VAR entitiesJson =新的对象[] {搜索,名称,描述,网址};
    返回JSON(entitiesJson,JsonRequestBehavior.AllowGet);
}
 


更新:

和为那些没有不耐烦的实际存储库进行测试:

 公众的ActionResult OpensearchJson(字符串搜索)
{
    VAR实体=新的[]
    {
        新{名称=的Xbox 360,说明=官方的Xbox从微软的网站,URL =htt​​p://www.xbox.com},
        新{名称=的Xbox秘籍,说明=codeS和演练,URL =htt​​p://www.example.com/xboxcheat$c$cs.aspx},
        新{名称=Xbox 360游戏,说明=游戏及配件,URL =htt​​p://www.example.com/games},
    };

    变种名称= entities.Select(米=> m.Name);
    Var描述= entities.Select(M => m.Description);
    VAR的网址= entities.Select(M => m.Url);
    VAR entitiesJson =新的对象[] {搜索,名称,描述,网址};
    返回JSON(entitiesJson,JsonRequestBehavior.AllowGet);
}
 

收益:

  [
    的Xbox,
    [
        Xbox 360的
        的Xbox秘籍,
        Xbox 360游戏
    ]
    [
        从微软官方的Xbox网站,
        codeS和演练,
        游戏及配件
    ]
    [
        http://www.xbox.com
        http://www.example.com/xboxcheat$c$cs.aspx
        http://www.example.com/games
    ]
]
 

这也正是预期的JSON。

Very likely a rather trivial question, but I simply couldn't find an appropriate answer. I want to return a "JsonResult" without the actual result having ANY property names whatsoever. Here is a little example of what I want to achieve:

["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"]]

Yes, some very ordinary source code already exists, but I doubt this is of any relevance. However, here it is:

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);
}

解决方案

Here you go:

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);
}


UPDATE:

and for those that are impatient to test without an actual repository:

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);
}

returns:

[
    "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"
    ]
]

which is exactly the expected JSON.

这篇关于返回JSON结果没有财产名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆