2014-12-05 128 views
4

我正在使用web api返回的JSON結構,但存在問題。Web API返回嵌套的JSON值

說,我有兩個表,TeamsPlayers。他們加入了TeamID(隊員和FK玩家PK)。

我希望我的API調用返回如下類似的一些JSON格式:

[ 
    { 
     TeamId: 1, 
     TeamName: 'Chicago Bulls', 
     TeamPlayers: [ 
      {PlayerId: 1, PlayerName: 'Pau Gasol'}, 
      {PlayerId: 2, PlayerName: 'Derrick Rose'}, 
      {PlayerId: 3, PlayerName: 'Joakim Noah'}, 
      {PlayerId: 4, PlayerName: 'Jimmy Butler'}, 
      {PlayerId: 5, PlayerName: 'Taj Gibson'}] 
    }, 
    { 
     TeamId: 2, 
     TeamName: 'Cleveland Cavaliers', 
     TeamPlayers: [ 
      {PlayerId: 1, PlayerName: 'Lebron James'}, 
      {PlayerId: 2, PlayerName: 'Kyrie Irving'}, 
      {PlayerId: 3, PlayerName: 'Anderson Varejao'}, 
      {PlayerId: 4, PlayerName: 'Dion Waiters'}, 
      {PlayerId: 5, PlayerName: 'Shawn Marion'}] 
    }, 
    { 
     TeamId: 3, 
     TeamName: 'Los Angeles Clippers', 
     TeamPlayers: [ 
      {PlayerId: 1, PlayerName: 'Chris Paul'}, 
      {PlayerId: 2, PlayerName: 'Blake Griffin'}, 
      {PlayerId: 3, PlayerName: 'DeAndre Jordan'}, 
      {PlayerId: 4, PlayerName: 'Jamal Crawford'}, 
      {PlayerId: 5, PlayerName: 'Matt Barnes'}] 
    } 
] 

控制器:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Threading.Tasks; 
using System.Web.Http; 
using System.Web.Http.Description; 
using MyApp.Models; 

namespace MyApp.Controllers 
{ 
    public class TeamsController : ApiController 
    { 
     private DataModel db = new DataModel(); 

     // GET: api/teams 
     public IQueryable<TeamsWithPlayers> GetTeamsAndPlayers() 
     { 
      var query = from x in db.Teams 
         join y in db.Players on x.TeamId equals y.TeamId 
         select 
         { 
          // This is where I need some help... 
         } 
     } 
    } 
} 

TeamAndPlayer類:

namespace MyApp.Models 
{ 
    public class TeamAndPlayers 
    { 
     public int TeamId { get; set; } 
     public string TeamName { get; set; } 
     public Players players { get; set; } 
    } 
} 

玩家等級:

namespace MyApp.Models 
{ 
    public class Players 
    { 
     public int TeamId { get; set; } 
     public int PlayerId { get; set; } 
     public string PlayerName { get; set; } 
    } 
} 

有人可以提供一些見解嗎?

+0

您的JSON結構與您的模型不匹配!在JSON中,您返回一組TeamPlayers,而您的TeamAndPlayers擁有一個Player,但名爲Players。 – 2014-12-07 10:39:34

回答

8
  • 我會假設JSON結構是真理的來源不是這裏的模型(TeamWithPlayers /播放器) - 見我的意見。

  • 我的解決方案假定您使用實體框架從數據庫中檢索數據,因爲我使用的是「包含」方法,但可以將其替換爲使用「加入」。

1-定義TeamDto和PlayerDto類如:

public class TeamDto 
{ 
    public int TeamId { get; set; } 
    public string TeamName { get; set; } 
    public IEnumerable<PlayerDto> TeamPlayers { get; set; } 
} 

public class PlayerDto 
{ 
    public int PlayerId { get; set; } 
    public string PlayerName { get; set; } 
} 

2 - 你TeamsController將是這樣的:

public class TeamsController : ApiController 
{ 
    private readonly TeamDbContext _dbContext = new TeamDbContext(); 

    // GET api/teams 
    public IEnumerable<TeamDto> GetTeamsAndPlayers() 
    { 
     var teams = _dbContext 
      .Teams 
      .Include("Players") // Load the players associated with each Team, (this depends on your declaration, but you mentioned that there is a FK from Player => TeamId) 
      // you can use the join if you like or if you don't use entity framework where you cannot call Include, but the following code will stay the same 
      .Select(t => new TeamDto 
      { 
       TeamId = t.TeamId, 
       TeamName = t.TeamName, 
       TeamPlayers = t.Players.Select(p => new PlayerDto 
        { 
         PlayerId = p.PlayerId, 
         PlayerName = p.PlayerName 
        }) 
      }).ToList(); 

     return teams; 
    } 
} 

希望有所幫助。

+0

如果您沒有FK關係,您可以請擴展您的答案以顯示它的外觀。謝謝 – 2015-04-30 01:02:33

+0

我的朋友說你真的很棒。謝謝。 – HPbyP 2017-11-23 15:18:54

0
var query = from x in db.Teams 
    join y in db.Players on x.TeamId equals y.TeamId 
    select new TeamsWithPlayers 
    { 
     TeamId = x.Id, 
     TeamName= x.TeamName, 
     Players = y 
    }.ToList(); 
+0

您的查詢不會返回所需的結構,請注意模型聲明和您的查詢,您會注意到您的查詢返回TeamsPlayer中的TeamId,並且Players不是集合。 – 2014-12-07 10:55:07