从后端的简洁查询中的对象中提取值
我从后端获取一个对象数组(oMessageData),如下所示:
foreach(var l in oMessageData)
{
Console.WriteLine(JsonConvert.SerializeObject(l));
}
结果之一是:
{some data}
{some data}
{some data}
{some data}
{"SetId":"1","ValueType":"FT","ObservationIdentifier":"COVID-19 qRT-PCR","ObservationValue":"Not Detected","ObservationResultStatus":"F","DateAndTimeOfObservation":"2022-02-02T22:53:26.28"}
{some data}
我想提取 DateAndTimeOfObservation
,但是当我执行 var date = oMessageData[4] 时.['DateAndTimeOfObservation']
,它表示预期标识符
。 我也无法得到它: oMessageData[4].values[5]
。
我如何提取该值?
这是0MessageData[4]的结构: oMessageData[4]
编辑:我从 dapper 查询中获取此数据,对象中的第一个元素是 DapperRow,例如这:
{{DapperRow, SetId = '1', ValueType = 'FT', ObservationIdentifier = 'COVID-19 qRT-PCR', ObservationValue = 'Not Detected', ObservationResultStatus = 'F', DateAndTimeOfObservation = '02-02-2022 22:53:26'}}
I am getting an array of objects(oMessageData) from my backend like this:
foreach(var l in oMessageData)
{
Console.WriteLine(JsonConvert.SerializeObject(l));
}
and one of the result is:
{some data}
{some data}
{some data}
{some data}
{"SetId":"1","ValueType":"FT","ObservationIdentifier":"COVID-19 qRT-PCR","ObservationValue":"Not Detected","ObservationResultStatus":"F","DateAndTimeOfObservation":"2022-02-02T22:53:26.28"}
{some data}
I want to extract DateAndTimeOfObservation
, but whenn I do var date = oMessageData[4].['DateAndTimeOfObservation']
, it says Identifier expected
.
I also couldn't get it with :oMessageData[4].values[5]
.
How do I extract that value?
This is the structure of 0MessageData[4]:
oMessageData[4]
Edit: I am getting this data from dapper query and the first element in object is DapperRow,like this:
{{DapperRow, SetId = '1', ValueType = 'FT', ObservationIdentifier = 'COVID-19 qRT-PCR', ObservationValue = 'Not Detected', ObservationResultStatus = 'F', DateAndTimeOfObservation = '02-02-2022 22:53:26'}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据屏幕截图,您会得到一个字典对象。这意味着目标记录不能像在数组中一样通过索引查找,而是通过键查找。
在屏幕截图中,Values 属性已展开。这些条目中的每一个都被分配给 Keys 属性中的一个键。使用此键您可以访问目标条目。
有关词典的更多详细信息,请参阅 https://learn.microsoft.com/de-de/dotnet/api/system.collections.generic.idictionary-2?view=net-6.0
According to the screenshot you get a dicionary object. This means that the target record is not lookable by an index, like in an array, but by a key.
On the screenshot the Values property is expanded. Each of these entries is assigned to a key from the Keys property. With this key you can access the target entry.
For more details on dictionaries, see https://learn.microsoft.com/de-de/dotnet/api/system.collections.generic.idictionary-2?view=net-6.0
(这个答案是基于我的错误印象而给出的,即
oMessageData
是序列化对象的集合。事实并非如此,从问题帖子中可以看出。)正如加布里埃尔一样指出,截图中显示的变量似乎是字典类型。我怀疑如果您展开
System.Collections.Generic.IDictionary.Keys
行(屏幕截图中第一个展开行上方的行),您将看到"DateAndTimeOfObservation "
被列为字典键之一。您声称屏幕截图显示了
oMessageData[4]
的结构;在这种情况下,您应该通过设置date
来获取所需的值,如下所示:或者,以更具描述性的方式;更清楚地显示如何获取
字典
中给定键
的值:(This answer was given based on my wrong impression that
oMessageData
is the collection of the serialized objects. That is not the case, as can be seen in the question post.)As Gabriel is pointing out, the variable displayed in the screenshot seems to be of a dictionary type. I suspect that if you expand the
System.Collections.Generic.IDictionary<string, object>.Keys
line (the line above the first expanded line in your screenshot), you will see"DateAndTimeOfObservation"
being listed as one of the dictionary keys.You claim that the screenshot displays the structure of
oMessageData[4]
; in that case, you should get the desired value by settingdate
as follows:Or, in a more descriptive manner; displaying more clearly how you obtain the value for a given
key
in adictionary
: