مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

lrrtm12

عضویت از 1395/09/04

راهنمایی برای تبدیل list به json

  • چهارشنبه 21 شهریور 1397
  • 15:50
تشکر میکنم

من از کد زیر برای درست کردن فایل جیسون توی asp.net webapi استفاده میکنم:

[HttpPost]
        [Route("api/Users/GetAllCategoryWithSub")]
        public object GetAllCategoryWithSub()
        {
            try
            {
                var getCat = from a in db.goodsGroups
                             where a.parentId == 1 && a.Id != 1
                             orderby a.Id
                             select a;

                var resultList = new List<AllCat>();

                foreach (var goodsGroup in getCat)
                {
                    var getSubOne = from a in db.goodsGroups
                                    where a.parentId == goodsGroup.Id && a.Id != 1
                                    select a;

                    foreach (var goodsSub in getSubOne)
                    {
                        var getSubSecond = from a in db.goodsGroups
                                           where a.parentId == goodsSub.Id && a.Id != 1
                                           select a;

                        foreach (var secondGoodsSub in getSubSecond)
                        {
                            var obj = new GetGoodCat
                            {
                                cat = new AllCat
                                {
                                    id = goodsGroup.Id,
                                    name = goodsGroup.title,
                                    image = goodsGroup.image,
                                    subLevelOne = new SubLevelOne
                                    {
                                        id = goodsSub.Id,
                                        name = goodsSub.title,
                                        image = goodsSub.image,
                                        subLevelTwo = new SubLevelTwo
                                        {
                                            id = secondGoodsSub.Id,
                                            name = secondGoodsSub.title,
                                            image = secondGoodsSub.image
                                        }
                                    }
                                }

                            };

                            resultList.Add(obj.cat);
                        }
                    }
                }

                return resultList;
            }
            catch (Exception)
            {
                return message.ProgramError();
            }
        }

و نتیجش شبیه زیر میشه:

[
    {
        "id": 2,
        "name": "نوشیدنی",
        "image": "test.png",
        "subLevelOne": {
            "id": 11,
            "name": "نوشابه",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 25,
                "name": "نوشابه پپسی",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 2,
        "name": "نوشیدنی",
        "image": "test.png",
        "subLevelOne": {
            "id": 12,
            "name": "آبمیوه",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 26,
                "name": "آبمیوه سن ایچ",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 3,
        "name": "کالای اساسی",
        "image": "test.png",
        "subLevelOne": {
            "id": 9,
            "name": "برنج",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 22,
                "name": "برنج محسن",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 3,
        "name": "کالای اساسی",
        "image": "test.png",
        "subLevelOne": {
            "id": 10,
            "name": "روغن",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 24,
                "name": "روغن لادن",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 4,
        "name": "تنقلات",
        "image": "test.png",
        "subLevelOne": {
            "id": 13,
            "name": "چیپس",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 27,
                "name": "چپیس مزمز",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 4,
        "name": "تنقلات",
        "image": "test.png",
        "subLevelOne": {
            "id": 14,
            "name": "پاستیل",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 28,
                "name": "پاستیل مزمز",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 5,
        "name": "کنسرو و غذای آماده",
        "image": "test.png",
        "subLevelOne": {
            "id": 15,
            "name": "تن ماهی",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 29,
                "name": "تن جنوب",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 5,
        "name": "کنسرو و غذای آماده",
        "image": "test.png",
        "subLevelOne": {
            "id": 16,
            "name": "کمپوت",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 30,
                "name": "کمپوت بهرام",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 6,
        "name": "چاشنی و افزودنی",
        "image": "test.png",
        "subLevelOne": {
            "id": 17,
            "name": "آبمیوه",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 31,
                "name": "آبمیوه مزمز",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 6,
        "name": "چاشنی و افزودنی",
        "image": "test.png",
        "subLevelOne": {
            "id": 18,
            "name": "زعفران",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 32,
                "name": "زعفران خراسان",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 7,
        "name": "لبنیات و پروتوئین",
        "image": "test.png",
        "subLevelOne": {
            "id": 19,
            "name": "شیر",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 33,
                "name": "شیر خسرو",
                "image": "testmy.png"
            }
        }
    },
    {
        "id": 7,
        "name": "لبنیات و پروتوئین",
        "image": "test.png",
        "subLevelOne": {
            "id": 20,
            "name": "ماست",
            "image": "mytest.png",
            "subLevelTwo": {
                "id": 34,
                "name": "ماست کریم",
                "image": "testmy.png"
            }
        }
    }
]

ولی من میخوام زیر دسته ها به صورت آرایه باشه. مثل کد زیر ( با [ , ] )

[
   {
      "id":1017,
      "name":"my name",
      "image":"image.png",
      "subGroupLevel2":[
         {
            "id":1017,
            "name":"my name",
            "image":"image.png",
            "subGroupLevel3":[
               {
                  "id":1017,
                  "name":"my name",
                  "image":"image.png"
               },
               {
                  "id":1017,
                  "name":"my name",
                  "image":"image.png"
               }
            ]
         }
      ]
   },
   {
      "id":1017,
      "name":"my name",
      "image":"image.png",
      "subGroupLevel2":[
         {
            "id":1017,
            "name":"my name",
            "image":"image.png",
            "subGroupLevel3":[
               {
                  "id":1017,
                  "name":"my name",
                  "image":"image.png"
               },
               {
                  "id":1017,
                  "name":"my name",
                  "image":"image.png"
               }
            ]
         },
         {
            "id":1017,
            "name":"my name",
            "image":"image.png",
            "subGroupLevel3":[
               {
                  "id":1017,
                  "name":"my name",
                  "image":"image.png"
               }
            ]
         }
      ]
   }
]

چیکار کنم که خروجی این شکلی بشه؟

پاسخ های این پرسش

تعداد پاسخ ها : 2 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • چهارشنبه 21 شهریور 1397
  • 17:34

باید داخل مدلی که دارید Serialize مینید یک لیست قرار دهید برای SubGroup ها که بعد از Serialize شدن به این شکل در میاد 

کاربر سایت

lrrtm12

عضویت از 1395/09/04

  • چهارشنبه 21 شهریور 1397
  • 23:18

این کلاس هایی هست که توی تابع بالا استفاده شده:
کجاش رو تغییر بدم؟

private class GetGoodCat
        {
            public AllCat cat;
        }
        private class AllCat
        {
            public int id;
            public string name;
            public string image;
            public SubLevelOne subLevelOne;
        }
        private class SubLevelOne
        {
            public int id;
            public string name;
            public string image;
            public SubLevelTwo subLevelTwo;
        }
        private class SubLevelTwo
        {
            public int id;
            public string name;
            public string image;
        }

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)