【问题标题】:Change feed iterator content deserialization更改提要迭代器内容反序列化
【发布时间】:2021-12-13 08:43:44
【问题描述】:

目前,我正在寻找使用 System.Text.Json 从 Change Feed Iterator 内容中反序列化实体的方法。

我从文档中找到了 this 示例,但它使用 Newtonsoft.Json,我不喜欢使用 JObject 和 JsonTextReader 的方法。 有什么办法可以让它更干净整洁吗?

我尝试过制作这样的东西,但我的方法不起作用。

ResponseMessage response = await iterator.ReadNextAsync(); 

var entities = await JsonSerializer.DeserializeAsync<IEnumerable<CosmosEntity1>>(response.Content);

【问题讨论】:

  • 在您的代码示例中什么不起作用?
  • @404 它抛出 System.Text.Json.JsonException 并显示一条消息“System.Text.Json.JsonException:JSON 值无法转换为 System.Collections.Generic.IEnumerable`1[ChangeFeedSubscriber .Entities.CosmosEntity1]".
  • 尝试将其反序列化到 List 中。但是,如果您向我们展示您的实体模型和响应内容中的 json 示例,则会更容易理解问题。
  • @Repulse3 不幸的是仍然有异常消息“无法将 JSON 值转换为 System.Collections.Generic.List`1[ChangeFeedSubscriber.Entities.CosmosEntity1]”。
  • 执行var json = new StreamReader(response.Content).ReadToEnd();时得到的JSON是什么?

标签: c# azure-cosmosdb system.text.json azure-cosmosdb-changefeed


【解决方案1】:

您获得的响应流不是文档的IEnumerable,而是包含文档作为其属性之一的类。尝试创建以下类:

public class ExampleResponse
{
    public string _rid { get; set; }
    public List<CosmosEntity1> Documents { get; set; }
    public int _count { get; set; }
}

并将您的流反序列化为该类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2011-03-09
    • 1970-01-01
    相关资源
    最近更新 更多