.net Core 2.0 Web API - Newtonsoft.Json.JsonSerializationException - IQueryable
.net Core 2.0 Web API - Newtonsoft.Json.JsonSerializationException - IQueryable
我有两个独立的 .net 核心应用程序,Web API 和客户端。我使用以下方法获取模型:
public IEnumerable<OhaMedication> GetOhaMedication()
{
return _context.OhaMedication;
}
型号:
public class OhaMedication
{
public int OhaMedicationId { get; set; }
public string Phn { get; set; }
public int OhaTypeId { get; set; }
public int FrequencyId { get; set; }
public MedFrequancy Frequency { get; set; }
public OhaType OhaType { get; set; }
}
public class OhaType
{
public OhaType()
{
OhaMedication = new HashSet<OhaMedication>();
}
public int OhaTypeId { get; set; }
public string Name { get; set; }
public ICollection<OhaMedication> OhaMedication { get; set; }
}
public class MedFrequancy
{
public MedFrequancy()
{
OhaMedication = new HashSet<OhaMedication>();
}
public int FrequencyId { get; set; }
public string Frequency { get; set; }
public ICollection<OhaMedication> OhaMedication { get; set; }
}
在客户端中,我使用以下方法获取数据:
public IQueryable<OhaMedication> GetohaMedication()
{
var dir = _session.GetString(SessionsKeys.Directory);
bool connection = InternetConnection.Check(_webApiData.WebApiitems.Url);
if (connection)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(_webApiData.WebApiitems.Url);
MediaTypeWithQualityHeaderValue contentType =
new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
HttpResponseMessage response = client.GetAsync("/OhaMedications").Result;
string stringData = response.Content.ReadAsStringAsync().Result;
IQueryable<OhaMedication> data = JsonConvert.DeserializeObject<IQueryable<OhaMedication>>(stringData);
return data;
}
}
else
return _context.OhaMedication;
}
我需要使用 IQueryable 来使用 include 如下:
var ohaMed = GetohaMedication().Where(x => x.Phn == phn).Include(o => o.Frequency)
.Include(o => o.OhaType).ToList();
我收到以下错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot create and populate list
type System.Linq.IQueryable`1[WebUI.Data.DataModels.OhaMedication].
Path '', line 1, position 1.'
它无法创建接口,因为不知道它实际应该使用哪种类型。
尝试:
JsonConvert.DeserializeObject<List<OhaMedication>>(stringData).AsQueryable()
我有两个独立的 .net 核心应用程序,Web API 和客户端。我使用以下方法获取模型:
public IEnumerable<OhaMedication> GetOhaMedication()
{
return _context.OhaMedication;
}
型号:
public class OhaMedication
{
public int OhaMedicationId { get; set; }
public string Phn { get; set; }
public int OhaTypeId { get; set; }
public int FrequencyId { get; set; }
public MedFrequancy Frequency { get; set; }
public OhaType OhaType { get; set; }
}
public class OhaType
{
public OhaType()
{
OhaMedication = new HashSet<OhaMedication>();
}
public int OhaTypeId { get; set; }
public string Name { get; set; }
public ICollection<OhaMedication> OhaMedication { get; set; }
}
public class MedFrequancy
{
public MedFrequancy()
{
OhaMedication = new HashSet<OhaMedication>();
}
public int FrequencyId { get; set; }
public string Frequency { get; set; }
public ICollection<OhaMedication> OhaMedication { get; set; }
}
在客户端中,我使用以下方法获取数据:
public IQueryable<OhaMedication> GetohaMedication()
{
var dir = _session.GetString(SessionsKeys.Directory);
bool connection = InternetConnection.Check(_webApiData.WebApiitems.Url);
if (connection)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(_webApiData.WebApiitems.Url);
MediaTypeWithQualityHeaderValue contentType =
new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
HttpResponseMessage response = client.GetAsync("/OhaMedications").Result;
string stringData = response.Content.ReadAsStringAsync().Result;
IQueryable<OhaMedication> data = JsonConvert.DeserializeObject<IQueryable<OhaMedication>>(stringData);
return data;
}
}
else
return _context.OhaMedication;
}
我需要使用 IQueryable 来使用 include 如下:
var ohaMed = GetohaMedication().Where(x => x.Phn == phn).Include(o => o.Frequency)
.Include(o => o.OhaType).ToList();
我收到以下错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot create and populate list type System.Linq.IQueryable`1[WebUI.Data.DataModels.OhaMedication]. Path '', line 1, position 1.'
它无法创建接口,因为不知道它实际应该使用哪种类型。 尝试:
JsonConvert.DeserializeObject<List<OhaMedication>>(stringData).AsQueryable()