我试图使用的WebAPI从我的数据的基础上获得的员工列表,使用此代码: 这是我的客户MVC应用程序的代码:无法反序列化当前的JSON对象,为什么?
string u = "http://localhost:1411/api/EmployeeAPI";
Uri uri = new Uri(u);
HttpClient httpClient = new HttpClient();
Task<HttpResponseMessage> response = httpClient.GetAsync(uri);
Task.WaitAll(response);
HttpResponseMessage resposta = response.Result;
var msg = resposta.Content.ReadAsStringAsync().Result;
Employee[] employees = JsonConvert.DeserializeObject<Employee[]>(msg);
return View(employees);
这是对我的代码的WebAPI:
public IEnumerable<Employee> GetEmployees()
{
return db.Employees.AsEnumerable();
}
但这种错误不断出现,我不明白为什么:
不能反序列化JSON当前对象(例如{ “名”: “值”}) 成型“DataAccess.Employee []”,因为类型要求JSON 阵列(例如[1,2,3])来正确地反序列化。为了解决这个问题错误 要么改变JSON到JSON阵列(例如[1,2,3]),或改变它的反序列化 类型,使得它是一个正常的.NET类型(例如不是 原始类型像整数,而不是一个集合类型等,其可以从一个JSON对象反序列化阵列或 列表)。 JsonObjectAttribute也可加入到将其从JSON对象强制 反序列化类型。路径 '消息',1号线,位置11
我Employee类:
namespace DataAccess
{
using System;
using System.Collections.Generic;
public partial class Employee
{
public Employee()
{
this.Products = new HashSet<Product>();
}
public int EmployeeId { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public byte[] rowguid { get; set; }
public System.DateTimeOffset ModifiedDate { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
}
的JSON输出我真的不知道如何得到它
味精变量的内容:
我的MSG可变回报
“{\” 消息\ “:\”。发生错误\ “\ ”ExceptionMessage \“:\” 是“ObjectConten t`1'类型未能序列化内容类型'application/json; \“ExceptionType \”:\“System.InvalidOperationException \”,\“StackTrace \”:null,\“InnerException \”:{\“Message \”:\“An error has发生“\”,\“ExceptionMessage \”:\“用'System.Data.Entity.DynamicProxies.ProductSubCategory_9EC9A3706390DE6A3B51F713F0DDAC2162AFB5B3FAB8F8587C9A865333A7729A'类型检测到的自回参考循环。路径 '[0]。产品[0] .ProductSubCategory.ProductCategory.ProductSubCategories' \ “\ ”ExceptionType \“:\ ”Newtonsoft.Json.JsonSerializationException \“,\ ”堆栈跟踪\“:\” 在Newtonsoft.Json。在JSONSerialization.JsonSerializerInternalWriter.SerializeList(JsonWriter编写器,IWrappedCollection值,JsonArrayContract合同,JsonProperty成员)中的Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter编写器,对象值,JsonProperty属性,JsonContract合同,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n ,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n位于Newtonsoft.Json.Serialization .JsonSerializerInternalWriter.SerializeObject(JsonWriter作家,对象VA JsonProperty成员JsonContainerContract collectionContract JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter作家,对象值,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty容器属性)\ r \在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter中的Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)\ n。(JsonWriter作家,对象值,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter作家,对象值,JsonObjectContract合同,JsonProperty成员,JsonContainerContract collectionContract, JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList (JsonWriter writer,IWrappedCollection values,JsonArrayContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object JsonProperty成员JsonContainerContract containerContract JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter作家,对象值,JsonObjectContract合同,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty容器属性)\ r \ Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IWrappedCollection values ,JsonArrayContract合同,JsonProperty成员,JsonContainerContract集合合同,JsonProperty容器属性)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter作家,对象值,JsonContract valueContract,Json属性成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value)\ r \ n在Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value) r \ n在System.Net.Http.Formatting.JsonMediaTypeFormatter。 <> c__DisplayClassd.b__c(个)\ r \ n在System.Threading.Tasks.TaskHelpers.RunSynchronously(行动行动,令牌的CancellationToken)\ “}}”
要做的第一件事:将WebAPI部分与客户端部分分开;取出JSON并检查它是否有意义。如果确实如此,则将其硬编码到一个小型客户端程序中,以隔离*这个问题。 –
你能发布无法反序列化的JSON输出吗? –
向我们展示您的JSON和您的“员工”类 –