C
C#2y ago
texail

❔ 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
Angius
Angius2y ago
So the format can be anything and everything, but it will have a url property somewhere? Deserializing won't really be feasible then, I fear
ACiDCA7
ACiDCA72y ago
looking at the json.. it looks pretty well structured, so creating classes for deserialization shouldnt be such a hurdle..
DaVinki
DaVinki2y ago
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
public static class ExpandoObjectHelper
{
public static void PrintRecursively(this ExpandoObject expandoObject, int depth = 0)
{
var indentation = new string('\t', depth);
foreach (var kvp in expandoObject)
{

if (kvp.Value is ExpandoObject subExpando)
{
Console.WriteLine($"{indentation}{kvp.Key}:");
subExpando.PrintRecursively(depth + 1);
continue;
}

Console.WriteLine($"{indentation}{kvp.Key} = {kvp.Value ?? "null"}");
}
}
}
public static class ExpandoObjectHelper
{
public static void PrintRecursively(this ExpandoObject expandoObject, int depth = 0)
{
var indentation = new string('\t', depth);
foreach (var kvp in expandoObject)
{

if (kvp.Value is ExpandoObject subExpando)
{
Console.WriteLine($"{indentation}{kvp.Key}:");
subExpando.PrintRecursively(depth + 1);
continue;
}

Console.WriteLine($"{indentation}{kvp.Key} = {kvp.Value ?? "null"}");
}
}
}
Just know the drawbacks of ExpandoObjects Using them is based
D.Mentia
D.Mentia2y ago
That 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
var reg = new Regex(@"""url"": ""([^""]*)""");
var matches = reg.Matches(jsonString);
foreach(Match m in matches)
Console.WriteLine(m.Groups[1]);
var reg = new Regex(@"""url"": ""([^""]*)""");
var matches = reg.Matches(jsonString);
foreach(Match m in matches)
Console.WriteLine(m.Groups[1]);
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 it
Accord
Accord2y ago
Was 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.
Want results from more Discord servers?
Add your server