我和这个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; }
}
我的上一次错误是。
消息=将值167转换为“Web.Domain.Ean.HotelCount”类型时出错 消息=无法转换或从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();
}
答案 0 :(得分:0)
真的不确定你有什么但是试试这个(猜测jToken属性 ActivePropertyCount 但是根据需要替换):
public string GetNumberOfHotels()
{
var numberOfHotelCount = GetHotelsFromEan.GetListOfHotels();
var root = JObject.Parse(numberOfHotelCount.ToString());
// Get list of hotels
IList<JToken> hotels = root["HotelListResponse"]["HotelList"].Children().ToList()
.Select(hotel => JsonConvert.DeserializeObject<HotelCount>(hotel.ToString())).ToList();
// Now get ActivePropertyCount for each hotel
int totalCount = 0;
foreach (JToken jToken in hotels)
{
totalCount += jToken.Value<int>("ActivePropertyCount");
}
return totalCount.ToString();
}