英文:
C# deserializator doesn't return result
问题
我在反序列化方面遇到问题,不知道如何处理。
var jsonString = @"
{
""Result"": [
{
""Id"": 994,
""Name"": ""LL Bottom Bracket"",
},
{
""Id"": 995,
""Name"": ""ML Bottom Bracket"",
}
]
}";
HotelRoomDTO 类看起来像这样:
public class HotelRoomDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
而服务是这样的:
public async Task<IEnumerable<HotelRoomDTO>> GetHotelRooms(string checkInDate, string checkOutDate)
{
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
var response = await _client.GetAsync($"api/hotelroom?checkInDate={checkInDate}&checkOutDate={checkOutDate}");
var content = await response.Content.ReadAsStringAsync();
var rooms = JsonConvert.DeserializeObject<IEnumerable<HotelRoomDTO>>(content, settings);
return rooms;
}
当我尝试运行应用程序时,反序列化器崩溃并且不返回结果。我认为问题在于这个 "Result" 对象列表。当它不存在时,一切正常,但当我从实际的数据库获取数据并尝试反序列化时,自动添加了这个 "Result",代码就无法正常工作。
英文:
i have problem with my deserialization and don't know how to deal.
var jsonString = @"
{
""Result"": [
{
""Id"": 994,
""Name"": ""LL Bottom Bracket"",
},
{
""Id"": 995,
""Name"": ""ML Bottom Bracket"",
}
]
}";
HotelRoomDTO class looks like:
public class HotelRoomDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
And Service is
public async Task<IEnumerable<HotelRoomDTO>> GetHotelRooms(string checkInDate, string checkOutDate)
{
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
var response = await _client.GetAsync($"api/hotelroom?checkInDate={checkInDate}& checkOutDate={checkOutDate}");
var content = await response.Content.ReadAsStringAsync();
var rooms = JsonConvert.DeserializeObject<IEnumerable<HotelRoomDTO>>(content, settings);
return rooms;
}
When i try to run app deserializator crash and doesnt return result. I think that problem is in this "Result" list of objects.When it doesnt exist everithing works fine but when i get data from real Db and try to deserialize it automatic adds this "Result" and code doesnt work
答案1
得分: 2
你可以尝试创建一个Root类:
public class Root
{
[JsonProperty("Result")]
public List<HotelRoomDTO> Result { get; set; }
}
然后进行反序列化:
var rooms = JsonConvert.DeserializeObject<Root>(jsonString, settings);
英文:
You can try a creting a Root class:
public class Root
{
[JsonProperty("Result")]
public List<HotelRoomDTO> Result { get; set; }
}
And deserialize him:
var rooms = JsonConvert.DeserializeObject<Root>(jsonString, settings);
答案2
得分: 2
你的问题在于你收到了一个"HotelRoomDTOs"列表,但你的模型只适用于一个。
Diego的回答在这种情况下有效。你想要做的是以下内容。将你的模型文件更改为以下内容:
public class Result
{
public int Id { get; set; }
public string Name { get; set; }
}
public class HotelRoomDTO
{
public List<Result> Result { get; set; }
}
在你的GetHotelRooms
函数中,现在将
var rooms = JsonConvert.DeserializeObject<IEnumerable<HotelRoomDTO>>(content, settings);
更改为
var rooms = JsonConvert.DeserializeObject<HotelRoomDTO>(content, settings);
变量rooms
现在是一个填充了HotelRoomDTOs的列表。
英文:
Your problem is that you are receiving a list of "HotelRoomDTOs", but your model is only made for one.
Diego's answer would work on this occasion. What you want to do is the following. Change your model file to the following:
public class Result
{
public int Id { get; set; }
public string Name { get; set; }
}
public class HotelRoomDTO
{
public List<Result> Result { get; set; }
}
In your GetHotelRooms
function you now change
var rooms = JsonConvert.DeserializeObject<IEnumerable<HotelRoomDTO>>(content, settings);
to
var rooms = JsonConvert.DeserializeObject<HotelRoomDTO>(content, settings);
The variable rooms
is now a list filled with HotelRoomDTOs.
答案3
得分: 0
我找到了最简单的解决方案,我移除了"Result",因为我不需要它,然后进行反序列化。感谢您提供的其他答案。
var result = JObject.Parse(content)["Result"].ToString(Formatting.None);
var rooms = JsonConvert.DeserializeObject<IEnumerable<HotelRoomDTO>>(content);
英文:
I found simplest solution i removed "Result" because i dont need it and after that desialize.Thank you for another answers.
var result = JObject.Parse(content)["Result"].ToString(Formatting.None);
var rooms = JsonConvert.DeserializeObject<IEnumerable<HotelRoomDTO>>(content);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论