System.Text.Json parsing property with dot in it
I have a third party server spewing a json response I need to parse. It is a single-string-property response with the property name containing a dot. How do I go about extracting the string value via System.Text.Json?
Ex:
With
jq
I would get at it via jq '.["m.server"]'
(where if the property was just "server", the syntax would just be jq '.server'
), but I have not come across this issue in a .net context before.4 Replies
If you are making a class to deserialize it to, you can add an attribute to change the name that it maps to. Have you tried that yet?
Yep.
[JsonPropertyName ("m.server")]
unfortunately has no effect for me.Hmmm
Have you tried parsing it to a JsonDocument instead? Maybe you could access it by name then
No. I am not familiar with that API. I'll give it a look. Thanks 🙂
That is indeed able to access the value via
JsonDocument.Parse (result).RootElement.GetProperty ("m.server").GetString ();
- thank you for pointing me there 🙂