The API I am working with requires JSON data, but also requires that the data be sent in the Content-Type of `application/x-www-form-urlencoded' so I am deserializing to a dictionary to pass the body to FormUrlEncodedContent. Everything was reasonably well with the world until I had to send a body that wasn't a flat structure.
I was reading the answer here, and thought that I had stumbled upon the solution to my problem, but I'm getting the following error:
Error reading string. Unexpected token: StartObject. Path 'filters[0]', line 1, position 165.
I am reasonably sure before I tried this method, the position was position 164, so at least I've made some progress before falling on my hands and knees here.
Here's an example of what I am doing.
Objects:
public partial class SearchRequest
{
[JsonProperty("id")]
public string Id { get; set; }
JsonProperty("fieldId")]
public string FieldId { get; set; }
[JsonProperty("fields")]
public string[] Fields { get; set; }
[JsonProperty("filters")]
public Filter[] Filters { get; set; }
[JsonProperty("pageNumber")]
public long PageNumber { get; set; }
[JsonProperty("searchText")]
public string SearchText { get; set; }
[JsonProperty("sorting")]
public Sorting[] Sorting { get; set; }
[JsonProperty("promptValues")]
public PromptValue[] PromptValues { get; set; }
}
public class Filter
{
[JsonProperty("fieldId")]
public string FieldId { get; set; }
[JsonProperty("operator")]
public string Operator { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
public class PromptValue
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("collectionValue")]
public string CollectionValue { get; set; }
[JsonProperty("promptId")]
public string PromptId { get; set; }
[JsonProperty("value")]
public Value Value { get; set; }
}
public class Value
{
}
public class Sorting
{
[JsonProperty("fieldId")]
public string FieldId { get; set; }
[JsonProperty("direction")]
public long Direction { get; set; }
}
Method:
Dictionary<string, string> dictBody;
SearchRequest searchRequest = new SearchRequest
{
// Do stuff
}
string body = Serialize.ToJson(searchRequest);
var jObj = JObject.Parse(body);
jObj["filters"] = JsonConvert.SerializeObject(jObj["filters"].ToObject<string[]>()); // Problem encountered here.
jObj["sorting"] = JsonConvert.SerializeObject(jObj["sorting"].ToObject<string[]>());
jObj["promptValues"] = JsonConvert.SerializeObject(jObj["promptValues"].ToObject<string[]>());
dictBody = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObj.ToString());
JSON Sample:
{
"id": "blah",
"fieldId": "blah",
"fields": [
"blah"
],
"filters": [
{
"fieldId": "blah",
"operator": "eq",
"value": "blah"
},
{
"fieldId": "blah",
"operator": "eq",
"value": "blah"
}
],
"pageNumber": "blah",
"searchText": "blah",
"sorting": [
{
"fieldId": "blah",
"direction": "eq"
}
],
"promptValues": [
{
"id": "blah",
"collectionValue": "blah",
"promptId": "blah",
"value": {}
}
]
}
Can someone help? Even better, is there way of automatically recognising the embedded objects and flattening them?

var dictBody = searchRequest.ToKeyValue();