【问题标题】:Convert an FeedResponse<Class> into List<Class> in DocumentDB在 DocumentDB 中将 FeedResponse<Class> 转换为 List<Class>
【发布时间】:2017-01-31 12:53:08
【问题描述】:

我正在尝试将 FeedResponse 转换为 List,但未能序列化字符串,因为它会引发错误

无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[Lutran.Api.Models.Infinity]',因为该类型需要 JSON 数组(例如 [1,2,3])正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像可以从 JSON 对象反序列化的数组或列表。 JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。 路径“令牌”,第 1 行,位置 9。

我已使用 this link 进行分页逻辑,并在返回整个输出对象时获取数据,但当我尝试转换它时失败。尝试使用 ienumerable 对象但它显示类型转换错误。使用动态对象,但无法从中提取 ResponseContinuation 值。

使用Newton Soft转换json(反序列化字符串)

var query = client.CreateDocumentQuery<Document>(collection, options).AsDocumentQuery();

if (query.HasMoreResults)
{
     var result = await query.ExecuteNextAsync<LeadDataView>();

     objLeadDataView.ResponseContinuation = result.ResponseContinuation;
     objLeadDataView.InfinityDataView = JsonConvert.DeserializeObject<List<Infinity>>(result.ToString());
     response = objLeadDataView;
}

【问题讨论】:

  • 您的异常信息是不言自明的,您可以使用 Newtonsoft Json 将您的 json 解析为 .net 对象
  • 请把代码贴在MVCE之后
  • @RahulSingh 检查代码,当我尝试仅使用来自 newtonsoft 的 jsonconvert 时出现该错误
  • @SainathNaidu - 是的,您的异常消息中提到了对吗?您正在传递一个集合,但您正在返回一个对象,请尝试使用 JsonConvert.DeserializeObject&lt;Infinity&gt;(result.ToString());
  • @RahulSingh objLeadDataView.InfinityDataView 是一个 List 对象,我尝试删除列表但显示错误“无法隐式转换类型”

标签: c# linq json.net deserialization azure-cosmosdb


【解决方案1】:

我想通了

public class LeadDataView
{
    public string ResponseContinuation { get; set; }

    public FeedResponse<Infinity> InfinityDataView { get; set; }
}

if (query.HasMoreResults)
            {
                var result = await query.ExecuteNextAsync<Infinity>();

                objLeadDataView.ResponseContinuation = result.ResponseContinuation;
                objLeadDataView.InfinityDataView = result;
                response = objLeadDataView;
            }

所以上面的代码在顶部发送了继续令牌,在下面发送了无穷大类数据。enter image description here

【讨论】:

    猜你喜欢
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    相关资源
    最近更新 更多