如何在Web Api 2中将LINQ嵌套列表添加到JSON?

时间:2016-05-24 13:52:07

标签: c# json linq asp.net-web-api

我正在创建一个返回Json的web api。我的出局看起来像这样:

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

我试图用这样的ouptut创建嵌套的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":[  
      {  
         "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看起来像第一个输出,我需要它看起来像第二个输出。 我完全迷失了,任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:4)

好吧,您可以尝试以下方法:

您的模特:

[17:06:00]:~$ opensc-tool -s ff:a4:00:00:01:05
Using reader with a card: ACS ACR38U-CCID 00 00
Sending: FF A4 00 00 01 05 
Received (SW1=0x6E, SW2=0x00)

您的方法:

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

希望它会有所帮助。