如何将Azure DocumentDB Document类转换为POCO类? [英] How to cast Azure DocumentDB Document class to my POCO class?

查看:62
本文介绍了如何将Azure DocumentDB Document类转换为POCO类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将Microsoft.Azure.Documents.Document对象转换为我的类类型?

Is there a way to cast the Microsoft.Azure.Documents.Document object to my class type?

我用CosmosDBTrigger编写了一个Azure Function类.触发器接收Microsoft.Azure.Documents.Document数组.我喜欢使用Document类,以便可以访问有关记录本身的元数据,但是我也想以静态方式与我的类类型中的数据进行交互.

I've written an Azure Function class, with a CosmosDBTrigger. The trigger receives an array of Microsoft.Azure.Documents.Document. I like having that Document class so that I can access the meta data about the record itself, but I also would like to interact with my data from my class type in a static way.

当我调用ToString时,我看到了数据的JSON表示形式.我应该使用Newtonsoft将该JSON手动转换为我的类类型吗?

I see the JSON representation of my data when I call ToString. Should I manually convert that JSON to my class type using Newtonsoft?

推荐答案

如果您需要在函数中将Document映射到POCO,那么最简单的方法就是您所建议的.

If you need to map your Document to your POCO in the function then the easiest way to do that is what you suggested.

调用document.Resource.ToString()方法并使用JSON.NET或您喜欢的json库中的DeserializeObject.推荐使用JSON.NET,因为Microsoft的CosmosDB库也要使用它.

Call the document.Resource.ToString() method and use DeserializeObject from JSON.NET or the json library you prefer. JSON.NET is recommended however as Microsoft's CosmosDB libraries use it as well.

您的映射调用将如下所示:

Your mapping call will look like this:

var yourPoco = JsonConvert.DeserializeObject<YourPocoType>(document.Resource.ToString())

这篇关于如何将Azure DocumentDB Document类转换为POCO类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆