我在 c# 中使用 botframework(Bot builder v3.8.x)来构建我的机器人,我想访问 LUIS 新 datetimeV2 实体中的值、开始和结束时间键的值。
在下面的 JSON 中,我想提取 "value": "2017-07-04", "start": "16:00:00" & “结束”:“18:00:00”
{
"entity": "july 4th",
"type": "builtin.datetimeV2.date",
"startIndex": 58,
"endIndex": 65,
"resolution": {
"values": [
{
"timex": "XXXX-07-04",
"type": "date",
"value": "2016-07-04"
},
{
"timex": "XXXX-07-04",
"type": "date",
"value": "2017-07-04"
}
]
}
},
{
"entity": "from 4 pm to 6 pm",
"type": "builtin.datetimeV2.timerange",
"startIndex": 67,
"endIndex": 84,
"resolution": {
"values": [
{
"timex": "(T16,T18,PT2H)",
"type": "timerange",
"start": "16:00:00",
"end": "18:00:00"
}
]
}
}
我正在尝试执行以下操作,但它给出了 Null Reference Exception
private const string DateEntityType = "builtin.datetimeV2.date";
private const string TimeRangeEntityType = "builtin.datetimeV2.timerange";
var entities = new List<EntityRecommendation>(result.Entities);
foreach (var entity in result.Entities)
{
switch (entity.Type)
{
case DateEntityType:
var date = entity.Resolution.Values.GetType().GetRuntimeProperty("value").ToString() ?? null;
break;
case TimeRangeEntityType:
var startTime = entity.Resolution.Values.GetType().GetRuntimeProperty("start").ToString() ?? null;
var endTime = entity.Resolution.Values.GetType().GetRuntimeProperty("end").ToString() ?? null;
break;
default:
break;
}
}
获取这些值的最简单方法是什么?
最佳答案
这肯定不是最优雅的方式,但我在 Newtonsoft 的一点帮助下设法从结构中获取数据:
if (entity.Type == "builtin.datetimeV2.daterange")
{
foreach (var vals in entity.Resolution.Values)
{
if (((Newtonsoft.Json.Linq.JArray)vals).First.SelectToken("type").ToString() == "daterange")
{
start = (DateTime)((Newtonsoft.Json.Linq.JArray)vals).First.SelectToken("start");
end = (DateTime)((Newtonsoft.Json.Linq.JArray)vals).First.SelectToken("end");
}
}
}
我希望你明白这一点。
关于c# - 如何在 botframework 中的 LUIS Json 字符串的新 "builtin.datetimeV2.timerange"(开始,结束)时间中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44022253/