独特的Json数据c#

时间:2018-04-24 13:46:14

标签: c# json asp.net-core

我已经在asp.net核心中创建了一个方法,以便在单个json数组中获取父名称及其子名称,但我不知道如何做到这一点我的方法给了我的结果如下

{
      "parent": "Kitchen & Dining ",
    "parentID": "QojQhZoUTkG1VhOMJVOBmw==",
    "child": "Bakeware",
    "childID": "BamlddxbUk2lx3uhaT4Hbg=="
},
{
      "parent": "Hobbies",
    "parentID": "yTCm3n8OCUSDQBzDt1f9Hw==",
    "child": "Travel",
    "childID": "CAqwuhPIL02T2ufP/Tl0CA=="
},
{
      "parent": "Kitchen & Dining ",
    "parentID": "QojQhZoUTkG1VhOMJVOBmw==",
    "child": "Table Linen",
    "childID": "EMz9RMY4RESyKtvFVAJTVQ=="
},
{
      "parent": "Kitchen & Dining ",
    "parentID": "QojQhZoUTkG1VhOMJVOBmw==",
    "child": "Tools and Gadgets",
    "childID": "FQtZRfDqtkGrn2II8HobZw=="
},
{
      "parent": "Kitchen & Dining ",
    "parentID": "QojQhZoUTkG1VhOMJVOBmw==",
    "child": "Dinnerware",
    "childID": "FXmgG6wf9UW9AC519gHuTw=="
},
{
      "parent": "Hobbies",
    "parentID": "yTCm3n8OCUSDQBzDt1f9Hw==",
    "child": "Camping & Trecking",
    "childID": "Kgj+Y1xdiUO2i79hxyDE6w=="
};

但我不想要这样的数据我希望它像这样:

{
      "parent": "Kitchen & Dining ",
    "parentID": "QojQhZoUTkG1VhOMJVOBmw==",
    "ChildData": [
      {
  "child": "Travel",
    "childID": "CAqwuhPIL02T2ufP/Tl0CA=="
  },
   {
  "child": "Table Linen",
    "childID": "EMz9RMY4RESyKtvFVAJTVQ"
  },
   {
  "child": "Tools and Gadgets",
    "childID": "FQtZRfDqtkGrn2II8HobZw=="
  }
  ]
},
{
      "parent": "Hobbies",
    "parentID": "yTCm3n8OCUSDQBzDt1f9Hw==",
    "ChildData":[
{"child": "Travel",
    "childID": "CAqwuhPIL02T2ufP/Tl0CA=="},
{"child": "Camping & Trecking",
    "childID": "Kgj+Y1xdiUO2i79hxyDE6w=="}
 ]
}

我该怎么做。我正在使用以下方法从c#asp.net core

中获取mysql的结果
var data = await (from p in _db.ProductCategoryRelationship
                    join s in _db.Products
                    on p.ProductId equals s.Id
                    where p.CategroyId == id
                    select s).ToListAsync();

有没有办法调整此方法以获得所需的结果或创建另一种方法可以给我我的结果

1 个答案:

答案 0 :(得分:0)

免责声明:我没有使用asp.net-core,所以我只是假设在该环境中可以使用常规LINQ。我也是从记忆中做到的。

假设您有办法将数据转换为您尚未向我们展示的JSON,看起来您有一个基本的数据转换问题。您需要将一个数据结构转换为另一个数据结构。

这就是LINQ非常方便的地方。

// Group by the parent information, so the output is a sequence of 
// collection records with a Key property (what you grouped on).

data2 = data.GroupBy(d => new { parent = d.parent, parentID = d.parentID})

// You can now create an anonymous type that has the parent information
// (as sub-fields in the Key property) and a collection of child
// records.

            .Select(g => new { parent = g.Key.parent,
                               parentID = g.Key.parentID,
                               ChildData = 
                                   g.Select(r => new { r.child,
                                                       r.childID }).ToArray()
                             });

您可能希望创建一个遵循此结构的命名类型(而不是我使用的匿名类型),而是在LINQ中创建它(而不是new - 建立一个匿名类型)。

class ParentType {
    string parent { get;set;}
    string parentID { get;set; }
    ChildType[] ChildData { get;set; }
}
class ChildType {
    string child { get;set; }
    string childID { get;set }
}

然后Select LINQ子句如下所示:

.Select(g => new ParentType() {
                   parent = g.Key.parent,
                   parentID = g.Key.parentID,
                   ChildData = g.Select( r => new ChildType() {
                          child = r.child,
                          childID = r.childID }).ToArray()
                })
.ToArray();