ChrisPasa ChrisPasa - 6 months ago 27
JSON Question

How do I LINQ nested lists to JSON in Web Api 2?

I am creating a web api that returns Json. My out put looks like this:

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


I Am trying to create nested Json with an ouptut like this:

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


This is my class:

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


And this is my controller:

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


My Json looks like the first output, I need it to look like the second output.
I totally lost, any help would be appreciated.

Answer

Well, you can try the following:

Your models:

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

Your method:

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

Hope it will help.