我在这个 json 上有点挣扎,我的第一个错误是:
$exception {"Error converting value 167 to type 'Web.Domain.Ean.HotelList'. Path '', line 1, position 3."} System.Exception {Newtonsoft.Json.JsonSerializationException}
所以我一直在尝试不同的方法来让它工作,现在我认为我已经把我的代码弄得一团糟,现在我得到了这个错误。
Cannot implicitly convert type 'string' to 'System.Collections.Generic.IEnumerable<Web.Domain.Ean.HotelCount>'
任何有一双新眼睛的人都可以看看,看看他们是否可以帮助解决错误,如果可能的话,还可以解释一些 json 理论来帮助我学习。
public class HotelCount
{
public string ActivePropertyCount { get; set; }
}
public IEnumerable<HotelCount> GetNumberOfHotels()
{
var numberOfHotelCount = GetHotelsFromEan.GetListOfHotels();
var root = JObject.Parse(numberOfHotelCount.ToString());
IList<JToken> hotelCount = root["HotelListResponse"]["HotelList"].Children().ToList();
IList<HotelCount> count = new List<HotelCount>();
foreach (JToken hotel in hotelCount)
{
HotelCount countHotels = JsonConvert.DeserializeObject<HotelCount>(hotel.ToString());
count.Add(countHotels);
}
var t = count.FirstOrDefault();
//IList<HotelCount> numberOfHotels = hotelCount.Select(count => JsonConvert
// .DeserializeObject<HotelCount>(
// count.SingleOrDefault().ToString())).ToList();
return t.ActivePropertyCount;
}
谢谢乔治
==========================更新代码======================= ========
public string GetNumberOfHotels()
{
var numberOfHotelCount = GetHotelsFromEan.GetListOfHotels();
var root = JObject.Parse(numberOfHotelCount.ToString());
IList<JToken> hotelCount = root["HotelListResponse"]["HotelList"].Children().ToList();
IList<HotelCount> count = hotelCount.Select(hotel => JsonConvert.DeserializeObject<HotelCount>(hotel.ToString())).ToList();
return count.ToString();
}
===============================新更新================== ===============
json 值的格式为 {"@activePropertyCount":"167","
所以我的课是:
public class HotelCount
{
public string ActivePropertyCount { get; set; }
}
我的最后一个错误是。
Message=将值 167 转换为类型“Web.Domain.Ean.HotelCount”时出错 Message=无法从 System.Int64 转换或转换为 Web.Domain.Ean.HotelCount。
所以如果我回到下面的代码,需要做些什么来返回 ActivePropertyCount 的值
public IEnumerable<HotelCount> GetNumberOfHotels()
{
var numberOfHotelCount = GetHotelsFromEan.GetListOfHotels();
var root = JObject.Parse(numberOfHotelCount.ToString());
IList<JToken> hotelCount = root["HotelListResponse"]["HotelList"].Children().ToList();
IList<HotelCount> count = new List<HotelCount>();
foreach (JToken hotel in hotelCount)
{
HotelCount countHotels = JsonConvert.DeserializeObject<HotelCount>(hotel.FirstOrDefault().ToString());
count.Add(countHotels);
}
return count;
}
======================修复了=========================
public string GetNumberOfHotels()
{
var numberOfHotelCount = GetHotelsFromEan.GetListOfHotels();
var root = JObject.Parse(numberOfHotelCount.ToString());
IList<JToken> hotelCount = root["HotelListResponse"]["HotelList"].Children().Values().ToList();
var s = hotelCount.FirstOrDefault();
var t = s;
// IList<HotelCount> count = hotelCount.Select(hotel => JsonConvert.DeserializeObject<HotelCount>(hotel.FirstOrDefault().ToString()).ActivePropertyCount).ToList();
return t.ToString();
}