C
C#3mo ago
PIE

Attempting to get value from different JSON files with variable key

Hello o/ I am attempting to deserilize and obtain the value from a variety of JSON files that contain similar values, but different keys with System.Text.Json. This was the current classes that I used in order to extract the values for one JSON file:
public class Info
{
[JsonPropertyName("user")]
public User User { get; }
[JsonPropertyName("galleryCount")]
public int GalleryCount { get; }
}

public class User
{
[JsonPropertyName("name")]
public string Name { get; }
[JsonPropertyName("account")]
public int Account { get; }
}
public class Info
{
[JsonPropertyName("user")]
public User User { get; }
[JsonPropertyName("galleryCount")]
public int GalleryCount { get; }
}

public class User
{
[JsonPropertyName("name")]
public string Name { get; }
[JsonPropertyName("account")]
public int Account { get; }
}
I know it's not possible to directly set attributes with a variable, but is there another way of dynamically searching for a value besides creating a class for every case? Thanks!
6 Replies
Jimmacle
Jimmacle3mo ago
if it's truly completely dynamic you can parse it into a JsonDocument but if you know ahead of time the schemas you're working with i'd just make normal models
Pobiega
Pobiega3mo ago
You can use Dictionary<string, ...> when you don't know the keys, but do know the value types
PIE
PIE3mo ago
I'm not sure what you mean by normal models, is it the same as the classes that I used above? Also there is an identifier from the JSON file that all have the same key that denotes the type of the JSON (all the JSON files with a "tumber" identifier will have the same keys). I could use it with a switch statement in order to parse preset keys in order to get the correct values as you said but I'm wondering if there is a better way...
Jimmacle
Jimmacle3mo ago
are they mostly the same? you could use polymorphic deserialization using that key as the type discriminator
PIE
PIE3mo ago
thanks I will look into it o/
Want results from more Discord servers?
Add your server
More Posts
How to display only department fields associated with a selected department in student automationhi, I'm facing a challenge while working on the student automation system. After selecting the deparHelp with DiscordGameSDK in a C# project (no Unity)I am currently trying to develop a plugin for a Unity game. For this I created a C# project, where IMarshal.GetActiveObject("Word.Application") fall in exceptionI'm working on a `.NET Framework` console application, I have added the `Microsoft.Office.Interop.WMigrating to Microsoft.IdentityModel.JsonWebTokens .net6 => .net8Hi, I am migrating a project which uses Duende as identity server + Ocelot gateway from .NET 6 to .NProcess working set is twice as commited managed memoryHi! I have an ASP.NET Core 8 app running on Linux in Docker. It's memory usage is limited by 512M. Resource based authorization with IdentityHello, I have the following setup in my asp.net core project: A `Workspace` model that has a bunch o✅ is .net 8.0 is not compatibleHey hi guys I'm new to this group and .net I just started learning .net I don't have any experience Help with why code is returning this way.Hello, I'm a CS student learning C# for the first time and i'm having some confusion on why my code Named pipe connection over network error (The username or password is incorrect)When running a solution where one machine runs a server and a second machine runs a client connectinDifference algorithm that Git usesHello! Quick rundown: I have a need to detect changes in custom bytecode sequences. Essentially, I