❔ Reading a Json-File into a List Of Objects
Hey, i want to read a JSON File into a List of Objects.
The JSON File looks like this:
I just shortened out the other letters.
How can i import the JSON File into a List of Objects?
37 Replies
How to serialize and deserialize JSON using C# - .NET
Learn how to use the System.Text.Json namespace to serialize to and deserialize from JSON in .NET. Includes sample code.
I read through this, but how do i create a List of objects with this?
did you read the "deserialization" part?
How to read JSON as .NET objects (deserialize) A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer.Deserialize method. For the generic overloads, you pass the type of the class you created as the generic type parameter. For the non-generic overloads, you pass the type of the class you created as a method parameter. You can deserialize either synchronously or asynchronously.
Thats the only part i read through, but it explains only how to create an object from a json looking like this
But i have stored multiple configurations in one json
and each of these "configurations" have their own structure?
in your example above they have the same structure
yeah, they have the same structure
Then just make a class that has that structure
and do
JsonSerializer.Deserialize<List<MyClass>>(jsonStringVariable);
oh, you have the "modes" wrapper, so just make a class that has that propI already tried, but i doesnt work out, i can send it in, but its very long due to 52 variables
... why are those not just auto-properties?
What do you mean?
public string A { get; set; }
no need for private string _A;
and public string A { get => _A; set => _A = value; }
just use public string A { get; set; }
, it does the same thingI found it on stackoverflow tbh, so i dont need that _A variables?
correct
is all you need
okay i changed that
is it called "Name" or "ConfigName" in your JSON?
ConfigName
but you want the .NET class property to just be "Name", right?
It doesnt make a difference
either do
or
i used this one then, just for more practise in changing that in .NET
okay
And this one?
Isnt it need then too?
your property is called "Modes"
so either use
JsonPropertyName
to change it, or just call it modesOkay changed it too
now all you need is
And configs is List Of Config then?
yup
Okay, thank you :)
Just one more question, is there a way i can check if the json has the correct format? And give an error or something?
Or give default values if a letter doesnt have a value in the config?
default value, just assign a default value in your model
ie
public string l { get; set; } = "default value";
check for error, well, the Deserialize method can return null
, so check for that
you'll get an exception if the json is wackyOkay, thank you. Is there a way to use this in .NET Framework and not .NET Core?
¯\_(ツ)_/¯
framework is dead and buried
Hm, okay, than i stick to Core. But how can I remove the other files from the Release Folder?
I mean this one "runtimeconfig.json"
if you want to "release" your app, use
dotnet publish
that will package it for distribution
you can even use single file publishing to make it a single .exe file
$singlefiledotnet publish -c Release -r <runtime identifier> -p:PublishSingleFile=true
Use of -r
|--runtime
implies --self-contained true
. Add --self-contained false
to publish as runtime-dependent.
-r RID
and -p:PublishSingleFile=true
can be moved to .csproj as the following properties:but to target multiple RIDs, you have to use dotnet publish
with the -r
option for each RID.
You can also add -p:IncludeNativeLibrariesForSelfExtract=true
to include native libraries (like Common Language Runtime dlls) in the output executable.
You might want to instead publish your application compiled Ahead Of Time to native code, see $nativeaot.
https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file
https://docs.microsoft.com/en-us/dotnet/core/rid-catalog
https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-publishCreate a single file for application deployment - .NET
Learn what single file application is and why you should consider using this application deployment model.
.NET Runtime Identifier (RID) catalog
Learn about the runtime identifier (RID) and how RIDs are used in .NET.
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.