首页 > 解决方案 > Angular 中无法读取来自 C# WebAPI 的错误 JSON 格式

问题描述

我正在尝试使用 HTTP Get 请求从 C# WebAPI 中提取数组

我使用以下代码获取列表

        [HttpGet]
        [Route("api/Events/GetEvents")]
        public List<Event> GetEvents()
        {
            db.Configuration.ProxyCreationEnabled = false;
            List<Event> getevents = db.Events.ToList();
            return getevents;
            
        }

这就是 Global.asx 中使用的 JSON 格式化程序的方式

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
                GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);

但这是我得到的数组

{"$id":"1","$values":[{"$id":"2","Event_id":1,"Event_Name":"Soccer","Event_Date":"2021-12-02T00:00:00","Fixtures":{"$id":"3","$values":[]}},{"$id":"4","Event_id":2,"Event_Name":"Rugby","Event_Date":"2021-12-03T00:00:00","Fixtures":{"$id":"5","$values":[]}}]}

有人知道我该如何解决这个问题吗?

标签: c#arraysjsonwebapi

解决方案


推荐阅读