When I run this code, I get an exception:
System.InvalidCastException: Unable to cast object of type 'System.Dynamic.ExpandoObject' to type 'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.KeyValuePair`2[System.String,System.String]]'.
I am trying to pass this to a POST
that takes parameters. Not sure how to do this?
using System.Dynamic; // ExpandObject
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
...
var s = @"
{
""time-in-force"": ""Day"",
""order-type"": ""Limit"",
""price"": ""2.0"",
""price-effect"": ""Credit"",
""legs"": [
{
""instrument-type"": ""Equity Option"",
""symbol"": ""SPY 191018C00298000"",
""quantity"": 1,
""action"": ""Buy to Open""
},
{
""instrument-type"": ""Equity Option"",
""symbol"": ""SPY 191018C00295000"",
""quantity"": 1,
""action"": ""Sell to Open""
}
]
}
";
try
{
dynamic json =
JsonConvert.DeserializeObject<ExpandoObject>(
s,
new ExpandoObjectConverter()
);
var iProps = json as IDictionary<string, Object>;
var furl = new FormUrlEncodedContent((IEnumerable<KeyValuePair<string, string>>)iProps);
}
catch(Exception ex)
{
}
CodePudding user response:
Just use "GetEnumerator" to get iProps as IEnumerable<KeyValuePair<string, object>> then you can iterate it and store it in List<KeyValuePair<string, string>>