久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='8mwNk'><tr id='8mwNk'><dt id='8mwNk'><q id='8mwNk'><span id='8mwNk'><b id='8mwNk'><form id='8mwNk'><ins id='8mwNk'></ins><ul id='8mwNk'></ul><sub id='8mwNk'></sub></form><legend id='8mwNk'></legend><bdo id='8mwNk'><pre id='8mwNk'><center id='8mwNk'></center></pre></bdo></b><th id='8mwNk'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='8mwNk'><tfoot id='8mwNk'></tfoot><dl id='8mwNk'><fieldset id='8mwNk'></fieldset></dl></div>
    • <bdo id='8mwNk'></bdo><ul id='8mwNk'></ul>

    <small id='8mwNk'></small><noframes id='8mwNk'>

      <tfoot id='8mwNk'></tfoot>
    1. <legend id='8mwNk'><style id='8mwNk'><dir id='8mwNk'><q id='8mwNk'></q></dir></style></legend>
    2. 從結(jié)構(gòu)化數(shù)據(jù)構(gòu)建 JSON 層次結(jié)構(gòu)

      Build JSON Hierarchy from Structured Data(從結(jié)構(gòu)化數(shù)據(jù)構(gòu)建 JSON 層次結(jié)構(gòu))

        <i id='1KGJW'><tr id='1KGJW'><dt id='1KGJW'><q id='1KGJW'><span id='1KGJW'><b id='1KGJW'><form id='1KGJW'><ins id='1KGJW'></ins><ul id='1KGJW'></ul><sub id='1KGJW'></sub></form><legend id='1KGJW'></legend><bdo id='1KGJW'><pre id='1KGJW'><center id='1KGJW'></center></pre></bdo></b><th id='1KGJW'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='1KGJW'><tfoot id='1KGJW'></tfoot><dl id='1KGJW'><fieldset id='1KGJW'></fieldset></dl></div>
      1. <tfoot id='1KGJW'></tfoot>
          <bdo id='1KGJW'></bdo><ul id='1KGJW'></ul>
          • <legend id='1KGJW'><style id='1KGJW'><dir id='1KGJW'><q id='1KGJW'></q></dir></style></legend>

                <tbody id='1KGJW'></tbody>
              • <small id='1KGJW'></small><noframes id='1KGJW'>

                本文介紹了從結(jié)構(gòu)化數(shù)據(jù)構(gòu)建 JSON 層次結(jié)構(gòu)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                C# |.NET 4.5 |實(shí)體框架 5

                我有從 SQL 查詢返回的 ID、ParentID、Name 形式的數(shù)據(jù).我想獲取該數(shù)據(jù)并將其解析為分層 JSON 字符串.到目前為止,這似乎是一項(xiàng)比應(yīng)有的艱巨任務(wù).由于我使用的是實(shí)體,因此數(shù)據(jù)作為 IEnumerable 很好地返回給我.現(xiàn)在我相信我只需要某種形式的遞歸,但我不太確定從哪里開始.任何幫助表示贊賞.

                數(shù)據(jù)返回為

                <上一頁>id parentId 名稱1 1 頂部位置2 1 位置 13 1 位置 24 2 位置1A

                代碼是

                public static string GetJsonLocationHierarchy(long locationID){使用(實(shí)體設(shè)置上下文 = 新實(shí)體設(shè)置()){//ID,ParentID,Name 的 IEnumerablecontext.GetLocationHierarchy(locationID);}}

                我希望的最終結(jié)果是這樣的:

                <代碼>{id":1",父ID":1","名稱": "TopLoc",孩子們": [{id":2",父ID":1",名稱":Loc1",孩子們": [{id":4",parentId":2",名稱":Loc1A",孩子們": [{}]}]},{id":3",父ID":1",名稱":Loc2",孩子們": [{}]}]}

                解決方案

                將平面表轉(zhuǎn)換為層次結(jié)構(gòu)的一種方法是將所有節(jié)點(diǎn)放入字典中.然后遍歷字典,并為每個節(jié)點(diǎn)查找其父節(jié)點(diǎn)并將其添加到父節(jié)點(diǎn)的子節(jié)點(diǎn).從那里,您只需要找到根并對其進(jìn)行序列化.

                這是一個演示該方法的示例程序:

                類程序{靜態(tài)無效主要(字符串 [] 參數(shù)){IEnumerable<Location>位置=新列表<位置>{新位置 { Id = 1,ParentId = 1,名稱 =TopLoc"},新位置 { Id = 2,ParentId = 1,名稱 =Loc1"},新位置 { Id = 3, ParentId = 1, Name = "Loc2" },新位置 { Id = 4, ParentId = 2, Name = "Loc1A" },};字典<int,位置>dict = locations.ToDictionary(loc => loc.Id);foreach(dict.Values 中的位置 loc){if (loc.ParentId != loc.Id){位置父 = dict[loc.ParentId];父.子.Add(loc);}}位置根 = dict.Values.First(loc => loc.ParentId == loc.Id);JsonSerializerSettings 設(shè)置 = 新的 JsonSerializerSettings{ContractResolver = new CamelCasePropertyNamesContractResolver(),格式 = 格式.縮進(jìn)};字符串 json = JsonConvert.SerializeObject(root, 設(shè)置);Console.WriteLine(json);}}上課地點(diǎn){公共位置(){孩子 = 新列表<位置>();}公共 int ID { 獲取;放;}公共 int ParentId { 獲取;放;}公共字符串名稱 { 獲取;放;}公共列表<位置>孩子{得到;放;}}

                這是輸出:

                <代碼>{身份證":1,父ID":1,"名稱": "TopLoc",孩子們": [{身份證":2,父ID":1,名稱":Loc1",孩子們": [{身份證":4,父ID":2,名稱":Loc1A",孩子們": []}]},{身份證":3,父ID":1,名稱":Loc2",孩子們": []}]}

                C# | .NET 4.5 | Entity Framework 5

                I have data coming back from a SQL Query in the form of ID,ParentID,Name. I'd like to take that data and parse it into a Hierarchical JSON string. So far it seems to be much more of a daunting task than it should be. Since I'm using Entity the data comes back nicely to me as an IEnumerable. Now I believe I just need some form of recursion, but I'm not quite sure where to start. Any help is appreciated.

                Data Returns as

                id   parentId    name
                1    1           TopLoc
                2    1           Loc1
                3    1           Loc2
                4    2           Loc1A
                

                Code is

                public static string GetJsonLocationHierarchy(long locationID)
                {
                    using (EntitiesSettings context = new EntitiesSettings())
                    {
                        // IEnumerable of ID,ParentID,Name
                        context.GetLocationHierarchy(locationID);
                    }
                }
                

                The end result I'd hope would be something like this:

                {
                    "id": "1",
                    "parentId": "1",
                    "name": "TopLoc",
                    "children": [
                        {
                            "id": "2",
                            "parentId": "1",
                            "name": "Loc1",
                            "children": [
                                {
                                    "id": "4",
                                    "parentId": "2",
                                    "name": "Loc1A",
                                    "children": [
                                        {}
                                    ]
                                }
                            ]
                        },
                        {
                            "id": "3",
                            "parentId": "1",
                            "name": "Loc2",
                            "children": [
                                {}
                            ]
                        }
                    ]
                }
                

                解決方案

                One way to turn a flat table into a hierarchy is to put all your nodes into a dictionary. Then iterate over the dictionary, and for each node, look up its parent and add it to the parent's children. From there, you just need to find the root and serialize it.

                Here is an example program to demonstrate the approach:

                class Program
                {
                    static void Main(string[] args)
                    {
                        IEnumerable<Location> locations = new List<Location>
                        {
                            new Location { Id = 1, ParentId = 1, Name = "TopLoc" },
                            new Location { Id = 2, ParentId = 1, Name = "Loc1" },
                            new Location { Id = 3, ParentId = 1, Name = "Loc2" },
                            new Location { Id = 4, ParentId = 2, Name = "Loc1A" },
                        };
                
                        Dictionary<int, Location> dict = locations.ToDictionary(loc => loc.Id);
                
                        foreach (Location loc in dict.Values)
                        {
                            if (loc.ParentId != loc.Id)
                            {
                                Location parent = dict[loc.ParentId];
                                parent.Children.Add(loc);
                            }
                        }
                
                        Location root = dict.Values.First(loc => loc.ParentId == loc.Id);
                
                        JsonSerializerSettings settings = new JsonSerializerSettings
                        {
                            ContractResolver = new CamelCasePropertyNamesContractResolver(),
                            Formatting = Formatting.Indented
                        };
                        string json = JsonConvert.SerializeObject(root, settings);
                
                        Console.WriteLine(json);
                    }
                }
                
                class Location
                {
                    public Location()
                    {
                        Children = new List<Location>();
                    }
                
                    public int Id { get; set; }
                    public int ParentId { get; set; }
                    public string Name { get; set; }
                    public List<Location> Children { get; set; }
                }
                

                Here is the output:

                {
                  "id": 1,
                  "parentId": 1,
                  "name": "TopLoc",
                  "children": [
                    {
                      "id": 2,
                      "parentId": 1,
                      "name": "Loc1",
                      "children": [
                        {
                          "id": 4,
                          "parentId": 2,
                          "name": "Loc1A",
                          "children": []
                        }
                      ]
                    },
                    {
                      "id": 3,
                      "parentId": 1,
                      "name": "Loc2",
                      "children": []
                    }
                  ]
                }
                

                這篇關(guān)于從結(jié)構(gòu)化數(shù)據(jù)構(gòu)建 JSON 層次結(jié)構(gòu)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標(biāo)簽的 XML)
                Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                Parse malformed XML(解析格式錯誤的 XML)
                • <small id='JTR06'></small><noframes id='JTR06'>

                    <bdo id='JTR06'></bdo><ul id='JTR06'></ul>
                          <tbody id='JTR06'></tbody>

                      • <legend id='JTR06'><style id='JTR06'><dir id='JTR06'><q id='JTR06'></q></dir></style></legend>

                          <i id='JTR06'><tr id='JTR06'><dt id='JTR06'><q id='JTR06'><span id='JTR06'><b id='JTR06'><form id='JTR06'><ins id='JTR06'></ins><ul id='JTR06'></ul><sub id='JTR06'></sub></form><legend id='JTR06'></legend><bdo id='JTR06'><pre id='JTR06'><center id='JTR06'></center></pre></bdo></b><th id='JTR06'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='JTR06'><tfoot id='JTR06'></tfoot><dl id='JTR06'><fieldset id='JTR06'></fieldset></dl></div>
                          <tfoot id='JTR06'></tfoot>

                          主站蜘蛛池模板: 日韩精品免费一区二区夜夜嗨 | 色天堂影院 | 综合网久久 | 九九热精品在线观看 | av在线免费观看网站 | 国产午夜精品视频 | 日韩在线视频观看 | 天天干网站 | 欧洲美一区二区三区亚洲 | 最新超碰| www.44| 婷婷99| 四虎wz| 日韩免费看片 | 欧美一级片在线播放 | 日韩精品久久久久久久酒店 | 蜜臀av中文字幕 | 久久成人免费视频 | 久久xxx | 四虎4hu永久免费网站影院 | 国内精品偷拍 | 亚洲男人天堂av | 亚洲福利在线观看 | 欧美夜夜操 | 日韩一区精品 | 日韩免费观看视频 | 亚洲小视频在线观看 | 日本黄a三级三级三级 | 国产精品无遮挡 | 亚洲免费黄色 | www.青青草 | 国产精品一区二区三区不卡 | 免费的av | 日韩视频二区 | 中文字幕无人区二 | 美女久久久久久 | 中文字幕超清在线观看 | 日本中文字幕一区 | 欧美一级欧美三级在线观看 | 亚洲欧美日韩成人 | 青青视频网 |