I have some Json that is returned from the Formidable Forms API but I am struggling to convert it to a (c#) list of objects. The returned json isn't an array of objects so when I attempt to deserialize to an object (using newtonsoft.json) I get an error:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List...because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
Here is a snip of the returned Json.
{
"mrjgb": {
"id": "50",
"item_key": "mrjgb",
"name": "John",
"ip": "",
"meta": {
"first_name": "John",
"middle_initial": "T",
"last_name": "Doe",
"date": "August 15, 2019",
"date-value": "2019-08-15"
},
"form_id": "15",
"post_id": "0",
"user_id": "0",
"parent_item_id": "0",
"is_draft": "0",
"updated_by": "0",
"created_at": "2019-08-15 18:10:59",
"updated_at": "2019-08-15 18:10:59"
},
"9rs0q": {
"id": "48",
"item_key": "9rs0q",
"name": "dsdds",
"ip": "",
"meta": {
"first_name": "dsdds",
"middle_initial": "",
"last_name": "23112qead",
"date": "August 13, 2019",
"date-value": "2019-08-13"
},
"form_id": "15",
"post_id": "0",
"user_id": "25",
"parent_item_id": "0",
"is_draft": "0",
"updated_by": "25",
"created_at": "2019-08-13 13:43:23",
"updated_at": "2019-08-13 13:43:23"
}
}
In this snip there are two objects, mrjgb and 9rs0q, but there could be any number of these objects and they have no set name.
Can someone please point me in the right direction? I've made many attempts with varying degrees of success.
//List<Foo> foo = JsonConvert.DeserializeObject<List<Foo>>( response.Content );
//var Foo = JsonConvert.DeserializeObject<List<Foo>>( response.Content, new FooConverter() );
//var results = JsonConvert.DeserializeObject<List<dynamic>>( response.Content );
var FooList = new List<Foo>();
var results = JsonConvert.DeserializeObject<dynamic>( response.Content );
//var results33 = JsonConvert.DeserializeObject<Foo>( results );
//var Foos = JsonConvert.DeserializeObject<List<Foo>>( results, new FooConverter() );
foreach (var token in results )
{
var Foo = JsonConvert.DeserializeObject<Foo>( token, new FooConverter() );
//var Foo = JsonConvert.DeserializeObject<Foo>( results, new FooConverter() ); // passes all tokens, not just the one we want to convert
FooList.Add( Foo );
}
//var Foo = JsonConvert.DeserializeObject<Foo>( response.Content );
EDIT:
With a little help from this post I was able to get a little bit closer. I can get a list of keys, but only if I know the source name (mrjgb in this case), but the name is random so I don't know it ahead of time....
JToken outer = JToken.Parse( response.Content );
JObject inner = outer[ "mrjgb" ].Value<JObject>();
List<string> keys = inner.Properties().Select( p => p.Name ).ToList();