Reputation: 1504
I have camel case JSON like this:
{
"name": "John",
"age": 55
}
I need to deserialize it into ExpandoObject but I would like the properties to be in Pascal Case. So I should be able to do this:
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json);
Console.WriteLine($"{obj.Name} is {obj.Age}");
Any ideas?
Upvotes: 6
Views: 8408
Reputation: 12351
My business code uses dynamic properties. So I need to use some form of DynamicObject.
IMHO - I get this, but at least to me, it doesn't explain the need for casing...
In any case, this was an interesting happy hour exercise...and I think this is janky and probably should never see the light of day :)
var json = "{ \"name\": \"John\", \"age\": 55, \"fooBoo\": 0}";
JObject obj = JObject.Parse(json);
dynamic foo = new ExpandoObject();
var bar = (IDictionary<string, object>) foo;
foreach (JProperty property in obj.Properties())
{
var janky = property.Name.Substring(0, 1).ToUpperInvariant() + property.Name.Substring(1);
bar.Add(janky, property.Value);
}
Console.WriteLine($"{foo.Name} , {foo.Age}, {foo.FooBoo}");
TGIF :)
Upvotes: 2