首页 > 解决方案 > 无法在轮播英雄卡中显示项目

问题描述

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

当我调试时,

显示的错误

System.ArgumentException:无法将数组转换为字符串。在 Newtonsoft.Json.Linq.JToken.op_Explicit(JToken 值)

这里有我的 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_name”和“text”。

感谢高级。

标签: botframeworkazure-bot-service

解决方案


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

这是给出问题的特定行:

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

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

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

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

拥有该数组后,您需要决定如何处理它。例如,您可能想用“;”将它们全部连接起来,例如,“test 1”和“test 2”将显示为:“test 1; test 2”作为最终描述。像这样的东西看起来像:

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

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

希望有帮助


推荐阅读