gpt4 book ai didi

botframework - 无法在 Carousel Hero Card 中显示项目

转载 作者:行者123 更新时间:2023-12-04 05:11:54 26 4
gpt4 key购买 nike

目前我想将 JSON 转换为 Carousel HeroCard,

当我调试时,

显示的错误是:

System.ArgumentException: Can not convert Array to String.
at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)

这里是我的 JSON:

{
"@odata.context": "https://Cognitivesearch.search.windows.net/indexes('testindex1')/$metadata#docs(*)",
"value": [
{
"@search.score": 0.03393994,
"metadata_storage_name": "DRSK.xlsm",
"metadata_storage_path": "aHR0cHM6Ly9rcG1nc2VhcmNoc3RvcmFnZS5ibG9iLmNvcmUud2luZG93cy5uZXQvYmFzaWNkZW1vL0RSU0sueGxzbQ2",
"text": [
"Excel Template Excel Template Unique tickers: 2 Display Rows: Valid(8) .Invalid(0) o All(8) Company Name Ticker Country Sector Currency Announced Date (mm/dd/yy) 1. V R&R ICE CREAM RRR1 US Food & Bevera |GBP 2/14/12 @ 2. J R&R ICE CREAM RRR1 US Food & Bevera GBP 2/14/11 0 3. V RAR ICE CREAM RRR1 US Food & Bevera GBF 2/14/10 @ 4. R&R ICE CREAM RRR1 US Food & Bevera GBP 2/14/09 @ 5. V R&R ICE CREAM RRR1 US Food & Bevera GBP 2/14/08 @ 6. / R&R ICE CREAM RRR1 US Food & Bevera |GBP 2/14/07 0 3. V DUKE FIRST BA DUKETST US Banks USD 1/31/14 @ B. / DUKE FIRST BA DUKETST US Banks USD 10/22/13 @ 2 Export to Excel D Upload Cancel",
"Excel Template Excel Template Step 1: 10) Download Template Step 2: Fill Data in Template Option 1 - Enter data manually or Option 2 - Cut and paste from your Excel workshee Do not change format or order of the columns. Step 3: Highlight data, drag, and drop here To highlight your data in Excel, select headers and data in Excel. Next, move your mouse pointer to the border of the selected area till the mouse pointer changes to a plus (+) with arrows. Left-click and drag the selection into this popup. Close"
]
},

{
"@search.score": 0.0032757183,
"metadata_storage_name": "DRSK non financial private companies white paper.pdf",
"metadata_storage_path": "aHR0cHM6Ly9rcG1nc2VhcmNoc3RvcmFnZS5ibG9iLmNvcmUud2luZG93cy5uZXQvYmFzaWNkZW1vL0RSU0slMjBub24lMjBmaW5hbmNpYWwlMjBwcml2YXRlJTIwY29tcGFuaWVzJTIwd2hpdGUlMjBwYXBlci5wZGY1",
"text": [
"Bloomberg",
"Technical Default Grace Period Default Resolution Bankruptcy Failure to Pay Coupon Positive Resolution: Firm Attempts to Firm Survives Violation of Fix Situation Debt Covenants Negative Resolution: Default Bankruptcy Figure 1 - Time line for default",
"1-Yr Default Prob 17.2533 35 32.2807 30 25 20 15 10 Jun Sep Dec Mar Jun Sep Dec Mar Jun Sep 2007 2008 2009 Copyright 2016 Bloomberg Finance L.P. 12-Jan-2016 14:08:33",

]
}
]
}

这里是我的源代码

   try
{
var content = response.Content;
JObject jObj = JObject.Parse(jsoncontent);
JArray jarr = (JArray)jObj["value"];

var attachments = new List<Attachment>();
foreach (JToken item in jarr)
{
var filename = (string)item.SelectToken("metadata_storage_name");
var filepath = (string)item.SelectToken("metadata_storage_path");
var desc = (string)item.SelectToken("text");

var heroCard = new HeroCard(
title: filename,
subtitle: desc
).ToAttachment();
attachments.Add(heroCard);

}

var reply = MessageFactory.Carousel(attachments);
await turnContext.SendActivityAsync(reply);
}catch(Exception ex)
{
await turnContext.SendActivityAsync(ex.ToString());
}

我想在 HeroCard 中显示 metadata_storage_nametext

最佳答案

您遇到的问题是“文本”部分 - 在您的 JSON 中,它不是单个值,而是字符串值的数组。因此,您需要考虑您实际想要如何处理它。

这是给出问题的特定行:

var desc = (string)item.SelectToken("text");

要实际检索这些字符串值,您可以将它们作为字符串数组导入,如下所示:

string[] descriptions = item.SelectToken("text").ToObject<string[]>();

(我没有在上面的行中测试空值,对于“文本”,您可能应该这样做)。

获得该数组后,您需要决定如何处理它。例如,您可能希望将它们全部连接起来,并用“;”将它们分开,例如“测试 1”和“测试 2”将显示为:“测试 1;测试 2”作为最终描述。像这样的东西看起来像:

string[] descriptions = item.SelectToken("text").ToObject<string[]>();
var desc = string.Join("; ", descriptions);

这会给你一个连接到“desc”变量的“描述”,你可以在那个时候用它做你想做的事。

希望对你有帮助

关于botframework - 无法在 Carousel Hero Card 中显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59763673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com