【问题标题】:Converting list to json in C#在 C# 中将列表转换为 json
【发布时间】:2018-08-15 06:42:35
【问题描述】:

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

last code

但返回 JSON 有如下错误:

期望对象或数组,而不是字符串
多个 JSON 根元素

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

编辑

我用这个改变我的代码:

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

                        resultList.Add(new JavaScriptSerializer().Serialize(obj));
                    }

                    return resultList;
                }                   
            }

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

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

现在我的 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\"}}}"
]

但我想要像this这样的东西

还有(\)是json文件吗?

【问题讨论】:

  • 为什么不使用 Newtonsoft JSON.NET?
  • 这可能会解决您的问题Newtonsoft.Json.JsonConvert.SerializeObject(getCat);
  • 您可以使用Newtonsoft。它是使用 .NET 项目管理 json 对象的第三方工具。下载Newtonsoft.Json Nuget 并参考教程。它真的很容易。
  • @MohitShrivastava 为什么要像前任一样构建 JSON。有问题吗?
  • 不要自己创建 json 字符串。以某种方式构建您的对象,它们代表最终所需的 JSON 结构,然后在最顶部的元素上使用 JsonConvert.SerializeObject(),就像您已经为 resultList 所做的那样。顺便说一句,根据 JSON 规范,属性名称和字符串值必须用双引号 " 括起来,而不是像你一样用单引号 ' 括起来。

标签: c# json linq


【解决方案1】:

我建议您使用 Nuget 包上提供的 C# NewtonSoft Json 包。

你可以这样做:

 var resultList = new List<SearchGoods>();

还有:

resultList.Add(obj);

最后直接返回:

return JsonConvert.SerializeObject(resultList);

它应该会给你正确的结果。

【讨论】:

【解决方案2】:

我觉得你的方法类型应该是 JsonResult 这样的

  public JsonResualt GetAllGoodInCat([FromBody]GoodsCatId goodsCatId){}

并且 在返回方法中,您应该像这样返回 Json

return Json(model, JsonRequestBehavior.AllowGet);

【讨论】:

    【解决方案3】:

    您的 json 格式无效,这就是您收到所有这些错误的原因。你有“”包围你的json的每个对象和数组。只需将其删除即可。

    我已经清理了你的 json :

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

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      相关资源
      最近更新 更多