首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不能反序列化当前的JSON对象,为什么会出现此错误?

不能反序列化当前的JSON对象,为什么会出现此错误?
EN

Stack Overflow用户
提问于 2014-06-11 10:06:24
回答 1查看 3.6K关注 0票数 0

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

代码语言:javascript
运行
复制
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的代码:

代码语言:javascript
运行
复制
public IEnumerable<Employee> GetEmployees()
{
return db.Employees.AsEnumerable();
}

但是这个错误不断出现,我不明白为什么:

代码语言:javascript
运行
复制
Cannot deserialize the current JSON object (e.g. {"name":"value"}) 
into type 'DataAccess.Employee[]' because the type requires a 
JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either 
change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized 
type so that it is a normal .NET type (e.g. not a primitive type like integer, 
not a collection type like an array or List) 
that can be deserialized from a JSON object. 
JsonObjectAttribute can also be added to the type to force it to deserialize 
from a JSON object. Path 'Message', line 1, position 11.

我的雇员班:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
"{\"Message\":\"An error has occurred.\",\
"ExceptionMessage\":\"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.\",\
"ExceptionType\":\"System.InvalidOperationException\",\
"StackTrace\":null,\
"InnerException\":{\
"Message\":\"An error has occurred.\",\
"ExceptionMessage\":\"Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.ProductSubCategory_9EC9A3706390DE6A3B51F713F0DDAC2162AFB5B3FAB8F8587C9A865333A7729A'. 
Path '[0].Products[0].ProductSubCategory.ProductCategory.ProductSubCategories'.\",\
"ExceptionType\":\"Newtonsoft.Json.JsonSerializationException\",\
"StackTrace\":\" 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 
at 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 value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 
at 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 value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 
at 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 value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)\r\n 
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.b__c()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)\"}}"
EN

回答 1

Stack Overflow用户

发布于 2014-06-11 13:55:17

我认为,如果您查看异常消息,您可能会深入研究这个问题。

异常消息表示检测到自引用循环。您能查看/共享模型Products.ProductSubCategory.ProductCategory.ProductSubCategories' ==>吗?

代码语言:javascript
运行
复制
"ExceptionMessage\":\"Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.ProductSubCategory_9EC9A3706390DE6A3B51F713F0DDAC2162AFB5B3FAB8F8587C9A865333A7729A'. 
Path '[0].Products[0].ProductSubCategory.ProductCategory.ProductSubCategories'.\",\
"ExceptionType\":\"Newtonsoft.Json.JsonSerializationException\",\
"StackTrace\":\" 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24160166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档