Exception is raised during runtime whenever I try to cast "Resolution.Values" in "builtin.datetimeV2.daterange" type to Newtonsoft.Json.Linq.JArray after upgrading to Bot Builder 3.9.
```C#
//Getting all the entities of type builtin.datetimeV2.daterange into list.
var years = result.Entities.Where(x => x.Type.Equals("builtin.datetimeV2.daterange"));
foreach (var year in years)
{
foreach (var vals in year.Resolution.Values)
{
//Exception is raised when I try to cast vals to JArray.
if (((JArray)vals).First.SelectToken("type").ToString() == "daterange")
{
_startYear = (DateTime)((JArray)vals).First.SelectToken("start");
_endYear = (DateTime)((JArray)vals).First.SelectToken("end");
}
//custom logic ....
}
}
``` json
"entities": [
{
"entity": "attrition",
"type": "attrition",
"startIndex": 0,
"endIndex": 8,
"score": 0.74733305
},
{
"entity": "this year",
"type": "builtin.datetimeV2.daterange",
"startIndex": 14,
"endIndex": 22,
"resolution": {
"values": [
{
"timex": "2017",
"type": "daterange",
"start": "2017-01-01",
"end": "2018-01-01"
}
]
}
}
]
I need to get _startYear and _endYear values from "year.Resolution.Values" which contains list of JArray Objects in the form of
Invalid cast exception is raised when casting "EntityRecommendation.Resolution.Values" After upgrading to BotBuilder 3.9 .However, there were no issues when I was using BotBuilder 3.8.5.
Is there any way to read start and end values without casting it to Newtonsoft.Json.Linq.JArray type?
For reference are you able to cast to JObject in lieu of JArray? Also is this the actual JSON that is leading to the exception?
@JasonSowers - It still throws Invalid Cast Exception when I tried casting to JObject. Json posted in the post is not the actual json response but a part of it. The problem is with the "values" under "Resolution" key.
Is there any workaround for this?
At the risk of sounding stupid, you tried
(((JObject)vals).SelectToken("type").ToString() == "daterange")
and not the following, right?
(((JObject)vals).First.SelectToken("type").ToString() == "daterange")
Are you able to post your complete code to a repo so we can take a look?
Did you ever resolve this?
I am also getting the same error. Any solution for that.
I got the alternate solution by converting JSON value to C# Class Properties and Deserialize using JSONConverter Class.
Would you mind posting a code example of your workaround for others?
Here is the example code. Please follow the below mentioned steps:
1) Create a Class File with following structure. You can modify the structure based on JSON value. Similarly might be you have to create a class for TimeRange.
```cs
using System.Collections.Generic;
namespace BotXYZ
{
public class CustomDateRange
{
public string timex { get; set; }
public string type { get; set; }
public string start { get; set; }
public string end { get; set; }
}
public class RootObject
{
public List<CustomDateRange> values { get; set; }
}
}
2) Place the code in RootDialog.cs as per below:
if (entity.Type == "builtin.datetimeV2.daterange")
{
object jsonobject;
entity.Resolution.TryGetValue("values", out jsonobject);
string jsonstringvalue= JsonConvert.SerializeObject(jsonobject);
List<CustomDateRange> lstDateRangeObj = (List<CustomDateRange>)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstringvalue, typeof(List<CustomDateRange>));
foreach (CustomDateRange objCustomDateRange in lstDateRangeObj)
{
if (objCustomDateRange.start != null)
{
var start = objCustomDateRange.start;
}
if (objCustomDateRange.end != null)
{
var end = objCustomDateRange.end;
}
if (objCustomDateRange.timex != null)
{
var timex = objCustomDateRange.timex;
}
}
}
```
Hoping that issue has been resolved, closing it. Feel free to reopen if any questions.
Most helpful comment
Here is the example code. Please follow the below mentioned steps:
1) Create a Class File with following structure. You can modify the structure based on JSON value. Similarly might be you have to create a class for TimeRange.
```cs
using System.Collections.Generic;
namespace BotXYZ
{
public class CustomDateRange
{
public string timex { get; set; }
public string type { get; set; }
public string start { get; set; }
public string end { get; set; }
}
}
2) Place the code in RootDialog.cs as per below:
```