【发布时间】:2020-05-05 05:41:51
【问题描述】:
我无法转换为返回 json 而不是列表。我尝试使用 Newtonsoft.Json,但没有任何效果。要么报错,要么返回null。
[HttpGet]
public IActionResult GetUsers()
{
using (var context = new ProjectContext())
{
var MyEntity = context.User.ToList();
return MyEntity;
}
}
用户模型
using System;
using System.Collections.Generic;
namespace Project.Models
{
public partial class User
{
public User()
{
Order= new HashSet<Order>();
}
public int IdUser { get; set; }
public string Name{ get; set; }
public string Tel{ get; set; }
public float? Stars{ get; set; }
public virtual SellerSeller { get; set; }
public virtual ICollection<Order> Order{ get; set; }
}
}
MyEntity 返回的类型是 System.Collections.Generic.List
【问题讨论】:
-
你能告诉我们返回 JSON,
MyEntity的类型及其模型 -
先确保
MyEntity不为空,然后使用return Json(new {date = MyEntity , Status = "success"}) -
context.User是否返回了一些东西?MyEntity的价值是什么 -
当你使用 newtonsoft 时,列出的对象是否在其属性上具有序列化属性? [JsonProperty]
-
@JamesLoForti 我用
var json = jsonSerialiser.Serialize (aList) ;做到了。由于返回了错误的方法,我给出了一个错误。菜鸟错误。
标签: c# json list entity-framework entity-framework-core