'Can't display item in Carousel Hero Card
Currently I want to convert the JSON to Carousel HeroCard,
When I debug,
The error shown is:
System.ArgumentException: Can not convert Array to String.
at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)
Here with my 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",
]
}
]
}
And Here with my Source Code
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());
}
and
I want display metadata_storage_name
and text
in HeroCard
.
Solution 1:[1]
The problem you're having is with the "text" section - in your JSON, it's not a single value, it's an array of string values. As a result, you need to think how you actually want to handle that.
Here's the particular line giving the issue:
var desc = (string)item.SelectToken("text");
To actually retrieve these string values, you can import them as a string array, like so:
string[] descriptions = item.SelectToken("text").ToObject<string[]>();
(I'm not testing for null values in the line above, for "text", you should probably do that).
Once you have that array, you need to decide what to do with it. As an example, you might want to join them all, with a "; " to separate them, e.g "test 1" and "test 2" would come out as: "test 1; test 2" as the final description. Something like this would look like:
string[] descriptions = item.SelectToken("text").ToObject<string[]>();
var desc = string.Join("; ", descriptions);
that will give you a joined "description" into the "desc" variable, and you can do what you want with it at that point.
hope that helps
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Hilton Giesenow |