【发布时间】:2021-11-14 05:25:55
【问题描述】:
我目前在解析自适应卡片时遇到了以下问题。
这是卡片:
{
"type": "AdaptiveCard",
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.4",
"body": [
{
"type": "TextBlock",
"text": "{{DATE(${$root.AdditionalData['DUE-DATE']},COMPACT)}}",
"wrap": true
}
]
}
这是卡片内容:
{
"AdditionalData": {
"DUE-DATE": "2021-09-10T16:29:59Z"
}
}
代码:
c# on .NET Framework 4.7.2 其中layout 是带有上述卡片的字符串,content 是带有上述卡片内容的字符串:
AdaptiveCardTemplate template = new AdaptiveCardTemplate(layout);
string cardJson = template.Expand(content);
AdaptiveCardParseResult card = AdaptiveCard.FromJson(cardJson);
它崩溃了:
AdaptiveCards.AdaptiveSerializationException: 'Error reading string. Unexpected token: Undefined. Path 'text', line 1, position 137.'
JsonReaderException: Error reading string. Unexpected token: Undefined. Path 'text', line 1, position 137.
cardJson 上生成的 JSON 在 text 属性中看起来不对:
{"type":"AdaptiveCard","$schema":"http://adaptivecards.io/schemas/adaptive-card.json","version":"1.4","body":[{"type":"TextBlock","text":,"wrap":true}]}
我正在使用自适应卡 nuget 包:
- AdaptiveCards 2.7.2
- AdaptiveCards.Templating 1.2.
我是否遇到了解析错误? text 属性的值应为 10.9.2021。
在adaptivecards.io 的设计器中,出于某种原因,一切正常。有没有人有修复/解决方法?
【问题讨论】:
-
在 cardJson 上生成的 JSON 肯定是格式错误的。将其上传到jsonformatter.curiousconcept.com 或jsonlint.com,您将收到
"text": ,缺少属性值的错误。所以问题不在于解析,而在于 JSON 生成。对于cardJson中的"text"的值,您期望得到什么?是"{{DATE(2021-09-10T16:29:59Z, COMPACT)}}"吗? -
@dbc 该值应为 10.9.2021。通常自适应卡片会在给定的上下文中呈现它。它适用于在线自适应卡片设计器 (adaptivecards.io),但不适用于 .Net
标签: c# json .net adaptive-cards