如何在Web API中获取Json数组的名称 [英] How to get a Name to a Json Array in web API

查看:168
本文介绍了如何在Web API中获取Json数组的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.net Web API,这是我的get方法:



I have an ASP.net Web API and this is my get method:

LyricContext db = new LyricContext();
public IEnumerable<MusicDTO> Get()
{
    var json = from M in db.Musics
               select new MusicDTO
               {
                   ID = M.ID,
                   MusicName = M.MusicName,
                   Lyric = M.Lyric,
                   Albume = M.Albume.AlbumeName,
                   Artist = M.Albume.Artist.ArtistName
               };
    return json;





这回复我这样的事情:





this return me something like this:

[
{
    "ID": "9245cd1e-c161-4c0b-ab43-25cef43b48b1",
    "Lyric": "I'm on a boat",
    "MusicName": "on a boat",
    "Albume": "New Albume",
    "Artist": "example"
},
...
]



但是我需要我的数组有这样的名字:




but I need that my array has a name like this:

{
"musics":    [
{
    "ID": "9245cd1e-c161-4c0b-ab43-25cef43b48b1",
    "Lyric": "I'm on a boat",
    "MusicName": "on a boat",
    "Albume": "New Albume",
    "Artist": "example"
},
...
]







我该怎么办?




what should I do?

推荐答案

我在asp.net论坛得到答案:



http:/ /forums.asp.net/p/1940355/5525425.aspx?p=True&t=635163597391075469&pagenum=1 [ ^ ]
I get my Answer in asp.net forums :

http://forums.asp.net/p/1940355/5525425.aspx?p=True&t=635163597391075469&pagenum=1[^]


LyricContext db = new LyricContext();
public HttpResponseMessage  Get()
{
    var json = from M in db.Musics
               select new MusicDTO
               {
                   ID = M.ID,
                   MusicName = M.MusicName,
                   Lyric = M.Lyric,
                   Albume = M.Albume.AlbumeName,
                   Artist = M.Albume.Artist.ArtistName
               };
    //return json;

    if (json.Any())
    {
        return Request.CreateResponse(HttpStatusCode.OK, new { musics = json.OrderBy(c => c.ID) });
     }
    else 
    {
        return Request.CreateResponse(HttpStatusCode.NotFound, new { musics = "No Data Available" });

    }
}


这篇关于如何在Web API中获取Json数组的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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