简体   繁体   English

Newtonsoft.Json.JsonSerializationException:'无法反序列化当前JSON对象

[英]Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object

Error : 错误:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[MFBDCP.HotelList+geolocation]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. Newtonsoft.Json.JsonSerializationException:'无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型'System.Collections.Generic.List`1 [MFBDCP.HotelList + geolocation]',因为该类型需要一个JSON数组(例如[1,2,3])可正确反序列化。

My Code : 我的代码:

HotelResponse myDetails = JsonConvert.DeserializeObject<HotelResponse>(final);

Parent Class : 家长班:

public class HotelResponse
{
   public List<hotels> hotels { get; set; } = new List<hotels>();
   public DateTime checkin { get; set; }
}

Child Class: 儿童班:

public class hotels
{
   public string hotel_code { get; set; }
   public List<geolocation> geolocation { get; set; }
}

Sub Child Class: 子级:

public class geolocation
{
   public string longitude { get; set; }
   public string latitude { get; set; }
}

Please let me know how to define nested lists 请让我知道如何定义嵌套列表

You probably have a mistake in the input string, final. 您可能在最终输入字符串中有一个错误。 The error message indicates that one of the arrays (lists) is not formed correctly. 错误消息表明数组(列表)之一的格式不正确。 Since you have nested lists, it could be either the inner or outer array that is not formed correctly. 由于您具有嵌套列表,因此它可能是内部或外部数组的格式不正确。

What I would do is, create an actual HotelResponse object programatically, which contains the same property values as final. 我要做的是,以编程方式创建实际的HotelResponse对象,该对象包含与final相同的属性值。 Then, use JsonConvert.SerializeObject() to see what the Json should look like. 然后,使用JsonConvert.SerializeObject()查看Json的外观。 By comparing the results of JsonConvert.SerializeObject to final, you should be able to figure out what is wrong. 通过将JsonConvert.SerializeObject的结果与final进行比较,您应该能够找出问题所在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Newtonsoft.Json.JsonSerializationException:无法反序列化当前的JSON对象 - Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object Newtonsoft.Json.JsonSerializationException: &#39;无法反序列化当前的 JSON 数组 - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array Newtonsoft.Json.JsonSerializationException: '无法反序列化当前的 JSON object (例如 {"name":"value"}) - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) Newtonsoft.Json.JsonSerializationException - Newtonsoft.Json.JsonSerializationException 错误:Newtonsoft.Json.JsonSerializationException - Error: Newtonsoft.Json.JsonSerializationException Xamarin Newtonsoft.Json.JsonSerializationException: - Xamarin Newtonsoft.Json.JsonSerializationException: 尝试将 Json 反序列化为对象时,出现此异常“Newtonsoft.Json.JsonSerializationException” - Getting this exception "Newtonsoft.Json.JsonSerializationException", when trying to deserialize Json to object 尝试使用模型反序列化时出现 Newtonsoft.Json.JsonSerializationException 问题 - Newtonsoft.Json.JsonSerializationException problem while trying to deserialize with a model 反序列化JSON对象会引发Newtonsoft.Json.JsonSerializationException - Deserializing JSON object throws a Newtonsoft.Json.JsonSerializationException JsonSerializationException:无法反序列化当前的 JSON 对象 - JsonSerializationException: Cannot deserialize the current JSON object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM