C#反序列化器没有返回结果

huangapple go评论65阅读模式
英文:

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 = @&quot;
            {
              &quot;&quot;Result&quot;&quot;: [
                {
                  &quot;&quot;Id&quot;&quot;: 994,
                  &quot;&quot;Name&quot;&quot;: &quot;&quot;LL Bottom Bracket&quot;&quot;,
                },
                {
                  &quot;&quot;Id&quot;&quot;: 995,
                  &quot;&quot;Name&quot;&quot;: &quot;&quot;ML Bottom Bracket&quot;&quot;,
                }
              ]
            }&quot;;

HotelRoomDTO class looks like:

public class HotelRoomDTO
{

        public int Id { get; set; }
        public string Name { get; set; }

}

And Service is

public async Task&lt;IEnumerable&lt;HotelRoomDTO&gt;&gt; GetHotelRooms(string checkInDate, string checkOutDate)
{

    var settings = new JsonSerializerSettings
    {
                NullValueHandling = NullValueHandling.Ignore,
                MissingMemberHandling = MissingMemberHandling.Ignore
    };
    var response = await _client.GetAsync($&quot;api/hotelroom?checkInDate={checkInDate}&amp;  checkOutDate={checkOutDate}&quot;);
    var content = await response.Content.ReadAsStringAsync();
    var rooms = JsonConvert.DeserializeObject&lt;IEnumerable&lt;HotelRoomDTO&gt;&gt;(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(&quot;Result&quot;)]
    public List&lt;HotelRoomDTO&gt; Result { get; set; }

}

And deserialize him:

var rooms = JsonConvert.DeserializeObject&lt;Root&gt;(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&lt;Result&gt; Result { get; set; }
}

In your GetHotelRooms function you now change

var rooms = JsonConvert.DeserializeObject&lt;IEnumerable&lt;HotelRoomDTO&gt;&gt;(content, settings);

to

var rooms = JsonConvert.DeserializeObject&lt;HotelRoomDTO&gt;(content, settings);

The variable rooms is now a list filled with HotelRoomDTOs.

C#反序列化器没有返回结果

答案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)[&quot;Result&quot;].ToString(Formatting.None);
var rooms = JsonConvert.DeserializeObject&lt;IEnumerable&lt;HotelRoomDTO&gt;&gt;(content);

huangapple
  • 本文由 发表于 2023年2月6日 18:57:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定