gpt4 book ai didi

c# - 无法反序列化当前JSON对象,为什么?

转载 作者:行者123 更新时间:2023-11-30 14:10:25 24 4
gpt4 key购买 nike

我正在尝试使用WebApi使用以下代码从我的数据库中获取员工列表:
这是我的客户MVC应用程序的代码:

        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对象(例如{“ name”:“ value”})
  设置为“ DataAccess.Employee []”类型,因为该类型需要JSON
  数组(例如[1,2,3])正确反序列化。解决此错误
  将JSON更改为JSON数组(例如[1,2,3])或更改
  反序列化类型,以便它是普通的.NET类型(例如,非
  基本类型(例如整数),而不是集合类型(例如数组或
  列表),可以从JSON对象反序列化。
  还可以将JsonObjectAttribute添加到类型中以强制其
  从JSON对象反序列化。路径“消息”,第1行,位置11。


我的员工班级:

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变量内容:

我的味精变量返回

“ {\” Message \“:\”发生了一个错误。\“,\” ExceptionMessage \“:\”“ ObjectContent`1”类型无法序列化内容类型为'application / json;的响应主体; charset = utf-8'。\“,\” ExceptionType \“:\” System.InvalidOperationException \“,\” StackTrace \“:null,\” InnerException \“:{\” Message \“:\”错误有\“,\” ExceptionMessage \“:\”检测到类型为'System.Data.Entity.DynamicProxies.ProductSubCategory_9EC9A3706390DE6A3B51F713F0DDAC2162AFB5B3FAB8F8587C9A865333A7729A'的自引用循环。路径'[0] .Products [0] .ProductSubCategory.ProductCategory.ProductSubCategories'。\“,\” ExceptionType \“:\” Newtonsoft.Json.JsonSerializationException \“,\” StackTrace \“:\”在Newtonsoft.Json。序列化。 ,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newton.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract \ JperProtonizationSon。 .JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,O在Newton的bject值,JsonObjectContract合同,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n \ r \ n。 \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonWriter。值,Newson的JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n。 JsonProperty构件,JsonContainerContract collectionContract,JsonProperty containerProperty)在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter作家,对象的值,JsonContract valueContract,JsonProperty构件,JsonContainerContract containerContract,JsonProperty containerProperty个)\ r \ r \ n \ n在Newtonsoft.Json序列化。 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonProtain成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n erContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperton containerPron。 JsonSerializerInternalWriter.SerializeList(JsonWriter作家,IWrappedCollection值,JsonArrayContract合约,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerJ,ConConnsonConsulterInternalWriter(S。 containerContract,JsonProperty containerProperty)\ r \ n在Newton.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value)\ r \ n在Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter) r \ n在System.Net.Http.Formatting.JsonMediaTypeFormatter。<> c__DisplayClassd.b__c()\ r \ n在System.Threading.Tasks.TaskHelpers.RunSynchronously(动作,CancellationToken令牌)\“}}”

最佳答案

阻止该错误发生的最佳方法是:

在您的WebApi项目中,转到App_Start文件夹,并将以下四行代码添加到WebApiConfig中:

config.EnableSystemDiagnosticsTracing();
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
config.Formatters.Remove(config.Formatters.XmlFormatter);


并确保您已安装Json nuget软件包

关于c# - 无法反序列化当前JSON对象,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24126675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com