2016-05-24 69 views
0

我正在創建一個返回Json的web api。我出來放看起來是這樣的:如何在Web Api 2中將嵌套列表LINQ到JSON?

[ 
    { 
     "NodeID":1252, 
     "CASNumber":"1333-86-4", 
     "EINECSCode":"215-609-9", 
     "EUIndex":"215-609-9", 
     "Duty":"No", 
     "Prohibited":"No", 
     "Unwanted":"No", 
     "IsReach":"No", 
     "SubstanceName":"Carbon black", 
     "GroupName":"Renault Complete", 
     "Portion":0.100000 
    }, 
    { 
     "NodeID":1252, 
     "CASNumber":"1333-86-4", 
     "EINECSCode":"215-609-9", 
     "EUIndex":"215-609-9", 
     "Duty":"No", 
     "Prohibited":"No", 
     "Unwanted":"No", 
     "IsReach":"No", 
     "SubstanceName":"Carbon black", 
     "GroupName":"Renault Orange", 
     "Portion":0.100000 
    } 
] 

我試圖創建嵌套的Json像這樣的ouptut:

{ 
    "NodeID":1252, 
    "CASNumber":"1333-86-4", 
    "EINECSCode":"215-609-9", 
    "EUIndex":"215-609-9", 
    "Duty":"No", 
    "Prohibited":"No", 
    "Unwanted":"No", 
    "IsReach":"No", 
    "SubstanceName":"Carbon black", 
    "GroupName":[ 
     { 
     "name":"Renault Complete" 
     }, 
     { 
     "name":"Renault Orange" 
     } 
    ], 
    "Portion":0.100000 
} 

這是我的課:

public class BasicSubstances 
{ 
    public int NodeID { get; set; } 
    public string CASNumber { get; set; } 
    public string EINECSCode { get; set; } 
    public string EUIndex { get; set; } 
    public string Duty { get; set; } 
    public string Prohibited { get; set; } 
    public string Unwanted { get; set; } 
    public string IsReach { get; set; } 
    public string SubstanceName { get; set; } 
    public string GroupName { get; set; } 
    public decimal ?Portion { get; set; } 
} 

這是我的控制器:

public List<BasicSubstances> GetBasicSubstance(string partNumber, string version, int nodeID, int parentID) 
{ 
    IMDSDataContext dc = new IMDSDataContext(); 
    List<BasicSubstances> results = new List<BasicSubstances>(); 

    foreach (spGetBasicSubstanceResult part in dc.spGetBasicSubstance(partNumber, version, nodeID, parentID)) 
    { 
     results.Add(new BasicSubstances() 
      { 
       NodeID = part.NodeID, 
       CASNumber = part.CASNumber, 
       EINECSCode = part.EINECSCode, 
       EUIndex = part.EINECSCode, 
       Duty = part.Duty, 
       Prohibited = part.Prohibited, 
       Unwanted = part.Unwanted, 
       IsReach = part.ISReach, 
       SubstanceName = part.SynonymName, 
       GroupName = part.GroupName, 
       Portion = part.Portion 

      }); 

    } 
    return results; 
} 

我的Json看起來像第一個輸出,我需要它看起來像第二個輸出。 我完全失去了,任何幫助,將不勝感激。

+0

你怎麼可以肯定,除了'GroupName'所有屬性都會有相同的價值觀? – MaKCbIMKo

+0

它們將始終具有相同的值。在我的存儲過程中它只有一條記錄,我加入了一個表以獲取多個組名列表。 – ChrisPasa

+0

GroupName的類型應該是'public List GroupName {get;組; }'?你不能用字符串創建json數組。你需要在這裏做一些事情'GroupName = part.GroupName,' – Win

回答

4

那麼,你可以嘗試以下方法:

您的機型:

public class BasicSubstanceViewModel 
{ 
    public int NodeID { get; set; } 
    public string CASNumber { get; set; } 
    public string EINECSCode { get; set; } 
    public string EUIndex { get; set; } 
    public string Duty { get; set; } 
    public string Prohibited { get; set; } 
    public string Unwanted { get; set; } 
    public string IsReach { get; set; } 
    public string SubstanceName { get; set; } 
    public List<GroupName> GroupName { get; set; } 
    public decimal ?Portion { get; set; } 
} 

public class GroupName 
{ 
    public string Name { get; set; } 
} 

你的方法:

public BasicSubstanceViewModel GetBasicSubstance(string partNumber, string version, int nodeID, int parentID) 
{ 
    IMDSDataContext dc = new IMDSDataContext(); 
    var spResult = dc.spGetBasicSubstance(partNumber, version, nodeID, parentID).ToList(); 

    if(!spResult.Any()) 
    { 
     return null; 
    } 

    var firstPart = spResult[0]; 

    var result = new BasicSubstanceViewModel 
    { 
     NodeID = firstPart.NodeID, 
     CASNumber = firstPart.CASNumber, 
     EINECSCode = firstPart.EINECSCode, 
     EUIndex = firstPart.EINECSCode, 
     Duty = firstPart.Duty, 
     Prohibited = firstPart.Prohibited, 
     Unwanted = firstPart.Unwanted, 
     IsReach = firstPart.ISReach, 
     SubstanceName = firstPart.SynonymName, 
     GroupName = spResult.Select(p => new GroupName { Name = p.GroupName }).ToList(), 
     Portion = firstPart.Portion 
    }; 

    return result; 
} 

希望這將有助於。

+0

var firstPart = spResult [0];引發錯誤:錯誤\t \t 14不能應用與[]索引,以類型的表達式「System.Data.Linq.ISingleResult 」 – ChrisPasa

+0

只要編輯。再試一次。 – MaKCbIMKo

+0

工作完美,需要一個分號後:部分= firstPart.Portion } ;.它不會讓我編輯。謝謝 – ChrisPasa