I have JSON objects having embedded arrays - with no predefined strongly typed class to deserialize to. ExpandoObject deserialization with Json.Net works, but the array is deserialized to list, which is an issue for me. I need expndoobject with arrays. Is there any setting I could use with Json.NET to achieve this?
Example:
var obj = """
{
"name": "John",
"age": 18,
"grid": [
{
"type": "A",
"price": 13
},
{
"type": "B",
"price": 1
},
{
"type": "A",
"price": 17
}
]
}
""";
var engine = new Engine()
.Execute("function eval(value) { return value.grid.filter((it)=>it.type === 'A').map(it=>it.price).reduce((a,b)=>a+b) }");
dynamic v = JsonConvert.DeserializeObject<ExpandoObject>(obj, new ExpandoObjectConverter());
engine.Invoke("eval", v);
Where this library is used: https://github.com/sebastienros/jint Result:
And I need an array there, or otherwise the call fails ("Property 'filter' of object is not a function").
Using dynamic v= Newtonsoft.Json.Linq.JObject.Parse(obj);
I got this:
And still fails with: "Accessed JArray values with invalid key value: "filter". Int32 array index expected."
If I define classes for this sample:
class Inner
{
public string Type { get; set; }
public int Price { get; set; }
}
class X
{
public string Name { get; set; }
public int Age { get; set; }
public Inner[] Grid { get; set; }
}
it is parsed just fine (var v = JsonConvert.DeserializeObject<X>(obj);
) and the code returns what I am expecting. Not so when I use List<Inner>
instead of the array. Hence the problem is that it is not an array.
So I am looking for any solution that results in an array at that position.