❔ JSON deserialization unknown files
Hi, guys! Help me please.
I have a json file like this, you never know what kind of it will be next.
I need to find some pairs in it. Check if the "url" key exists, if so, get its value. And I don’t care where he stands, even in the first place. It is desirable to do this through deserialization. Thank you!
json file : https://pastecord.com/puvetuqujy
5 Replies
So the format can be anything and everything, but it will have a
url
property somewhere?
Deserializing won't really be feasible then, I fearlooking at the json.. it looks pretty well structured, so creating classes for deserialization shouldnt be such a hurdle..
A little late but you are able to deserialize into ExpandoObjects if the next json is unknown
For some reason System.Text.Json only does the first level but Newtonsoft.Json can parse the entire json where each object is its own ExpandoObject and all keys are properties to the object
You are able to recursively search an ExpandoObject since all of its properties are
KeyValuePair<string, object?>
I wrote this a while ago when first playing around with ExpandoObjects and json that you can check out and see what I mean by going through an Expando recursively
Just know the drawbacks of ExpandoObjects
Using them is basedThat json looks very specific. Just copy it into any json-to-c# converter and get the classes and search through them specifically, if you're set on deserialization. The classes are probably even already available somewhere, it's minecraft
Or just
expando objects work too. But if you serialize, you just have to check
baseObject.downloads
, baseObject.libraries
, baseObject.logging
, those are all just arrays of basically the same object with a url and sha on itWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.