在 C# 中将列表转换为 json

我使用此方法从列表中生成 JSON:


last code

但返回 JSON 有如下错误:


期望对象或数组,而不是字符串

多个 JSON 根元素


您在此链接中看到 JSON 文件并在此站点中对其进行测试:


编辑:


我用这个更改我的代码:


[HttpPost]

    [Route("api/Users/GetAllGoodInCat")]

    public object GetAllGoodInCat([FromBody]GoodsCatId goodsCatId)

    {

        try

        {

            if (goodsCatId.id != 0)

            {

                var getCat = (from a in db.goodsGroups

                                 where a.Id == goodsCatId.id

                                 select a).SingleOrDefault();


                if (getCat != null)

                {

                    var getAllfood = from a in db.goods

                        where a.groupId == goodsCatId.id

                        orderby a.Id

                        select a;


                    var resultList = new List<string>();


                    foreach (var good in getAllfood)

                    {

                        var obj = new SearchGoods()

                        {

                            good = new MyGoods

                            {

                                id = good.Id,

                                name = good.name,

                                price = good.price,

                                brand = new MyGoodsBrand

                                {

                                    id = getCat.Id,

                                    name = getCat.title,

                                    image = getCat.image

                                }

                            }

                        };


现在我的 json 就像这个链接:


[

   "{\"good\":{\"id\":1,\"name\":\"برنج دانه بلند محسن\",\"price\":20000,\"brand\":{\"id\":22,\"name\":\"برنج محسن\",\"image\":\"testmy.png\"}}}",

   "{\"good\":{\"id\":2,\"name\":\"برنج عطری\",\"price\":30000,\"brand\":{\"id\":22,\"name\":\"برنج محسن\",\"image\":\"testmy.png\"}}}",

   "{\"good\":{\"id\":3,\"name\":\"برنج سر سیاه\",\"price\":15000,\"brand\":{\"id\":22,\"name\":\"برنج محسن\",\"image\":\"testmy.png\"}}}"

]

但我想要这样的东西


还有 (\) 是 json 文件吗?


不负相思意
浏览 341回答 3
3回答

狐的传说

我建议您使用Nuget&nbsp;Package 上提供的 C#&nbsp;NewtonSoft Json&nbsp;Package。你可以这样做:&nbsp;var&nbsp;resultList&nbsp;=&nbsp;new&nbsp;List<SearchGoods>();和:resultList.Add(obj);最后只返回:return&nbsp;JsonConvert.SerializeObject(resultList);它应该给你正确的结果。

回首忆惘然

我认为你的方法类型应该是 JsonResult 像这样&nbsp;&nbsp;public&nbsp;JsonResualt&nbsp;GetAllGoodInCat([FromBody]GoodsCatId&nbsp;goodsCatId){}在返回方法中,您应该像这样返回 Jsonreturn&nbsp;Json(model,&nbsp;JsonRequestBehavior.AllowGet);

慕尼黑5688855

您的 json 格式无效,这就是您收到所有这些错误的原因。你有 "" 包围了你的 json 的每个对象和数组。只需删除它,你就可以开始了。我已经清理了你的 json :[&nbsp; {&nbsp; &nbsp; "id": 2,&nbsp; &nbsp; "name": "نوشیدنی",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 11,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 2,&nbsp; &nbsp; &nbsp; &nbsp; "name": "نوشابه",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 25,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 11,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "نوشابه پپسی",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 2,&nbsp; &nbsp; "name": "نوشیدنی",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 12,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 2,&nbsp; &nbsp; &nbsp; &nbsp; "name": "آبمیوه",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 26,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 12,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "آبمیوه سن ایچ",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 3,&nbsp; &nbsp; "name": "کالای اساسی",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 9,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 3,&nbsp; &nbsp; &nbsp; &nbsp; "name": "برنج",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 22,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 9,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "برنج محسن",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 3,&nbsp; &nbsp; "name": "کالای اساسی",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 10,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 3,&nbsp; &nbsp; &nbsp; &nbsp; "name": "روغن",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 24,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 10,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "روغن لادن",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 4,&nbsp; &nbsp; "name": "تنقلات",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 13,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 4,&nbsp; &nbsp; &nbsp; &nbsp; "name": "چیپس",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 27,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 13,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "چپیس مزمز",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 4,&nbsp; &nbsp; "name": "تنقلات",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 14,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 4,&nbsp; &nbsp; &nbsp; &nbsp; "name": "پاستیل",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 28,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 14,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "پاستیل مزمز",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 5,&nbsp; &nbsp; "name": "کنسرو و غذای آماده",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 15,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 5,&nbsp; &nbsp; &nbsp; &nbsp; "name": "تن ماهی",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 29,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 15,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "تن جنوب",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 5,&nbsp; &nbsp; "name": "کنسرو و غذای آماده",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 16,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 5,&nbsp; &nbsp; &nbsp; &nbsp; "name": "کمپوت",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 30,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 16,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "کمپوت بهرام",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 6,&nbsp; &nbsp; "name": "چاشنی و افزودنی",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 17,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 6,&nbsp; &nbsp; &nbsp; &nbsp; "name": "آبمیوه",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 31,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 17,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "آبمیوه مزمز",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 6,&nbsp; &nbsp; "name": "چاشنی و افزودنی",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 18,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 6,&nbsp; &nbsp; &nbsp; &nbsp; "name": "زعفران",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 32,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 18,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "زعفران خراسان",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 7,&nbsp; &nbsp; "name": "لبنیات و پروتوئین",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 19,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 7,&nbsp; &nbsp; &nbsp; &nbsp; "name": "شیر",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 33,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 19,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "شیر خسرو",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 7,&nbsp; &nbsp; "name": "لبنیات و پروتوئین",&nbsp; &nbsp; "image": "test.png",&nbsp; &nbsp; "subLevelOne": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 20,&nbsp; &nbsp; &nbsp; &nbsp; "parentId": 7,&nbsp; &nbsp; &nbsp; &nbsp; "name": "ماست",&nbsp; &nbsp; &nbsp; &nbsp; "image": "mytest.png",&nbsp; &nbsp; &nbsp; &nbsp; "subLevelTwo": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 34,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parentId": 20,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "ماست کریم",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image": "testmy.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; }]希望能帮到你!
打开App,查看更多内容
随时随地看视频慕课网APP