C
C#16mo ago
Unicursal

❔ 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:
{
"Modes": [
{
"ConfigName": "Small Uppertext",
"A": "ᴬ",
"B": "ᴮ",
"y": "ʸ",
"z": "ᶻ"
},
{
"ConfigName": "Something Else",
"A": "ᴬ",
"B": "ᴮ",
"y": "ʸ",
"z": "ᶻ"
}
]
}
{
"Modes": [
{
"ConfigName": "Small Uppertext",
"A": "ᴬ",
"B": "ᴮ",
"y": "ʸ",
"z": "ᶻ"
},
{
"ConfigName": "Something Else",
"A": "ᴬ",
"B": "ᴮ",
"y": "ʸ",
"z": "ᶻ"
}
]
}
I just shortened out the other letters. How can i import the JSON File into a List of Objects?
37 Replies
Pobiega
Pobiega16mo ago
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.
Unicursal
Unicursal16mo ago
I read through this, but how do i create a List of objects with this?
Pobiega
Pobiega16mo ago
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.
Unicursal
Unicursal16mo ago
Thats the only part i read through, but it explains only how to create an object from a json looking like this
{
"ConfigName": "Something Else",
"A": "ᴬ",
"B": "ᴮ",
"y": "ʸ",
"z": "ᶻ"
}
{
"ConfigName": "Something Else",
"A": "ᴬ",
"B": "ᴮ",
"y": "ʸ",
"z": "ᶻ"
}
But i have stored multiple configurations in one json
Pobiega
Pobiega16mo ago
and each of these "configurations" have their own structure? in your example above they have the same structure
Unicursal
Unicursal16mo ago
yeah, they have the same structure
Pobiega
Pobiega16mo ago
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 prop
Unicursal
Unicursal16mo ago
I already tried, but i doesnt work out, i can send it in, but its very long due to 52 variables
Pobiega
Pobiega16mo ago
... why are those not just auto-properties?
Unicursal
Unicursal16mo ago
What do you mean?
Pobiega
Pobiega16mo ago
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 thing
Unicursal
Unicursal16mo ago
I found it on stackoverflow tbh, so i dont need that _A variables?
Pobiega
Pobiega16mo ago
correct
public string Name { get; set; }
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public string E { get; set; }
public string F { get; set; }
public string G { get; set; }
public string H { get; set; }
public string I { get; set; }
public string J { get; set; }
public string K { get; set; }
public string L { get; set; }
public string M { get; set; }
public string N { get; set; }
public string O { get; set; }
public string P { get; set; }
public string Q { get; set; }
public string R { get; set; }
public string S { get; set; }
public string T { get; set; }
public string U { get; set; }
public string V { get; set; }
public string W { get; set; }
public string X { get; set; }
public string Y { get; set; }
public string Z { get; set; }
public string a { get; set; }
public string b { get; set; }
public string c { get; set; }
public string d { get; set; }
public string e { get; set; }
public string f { get; set; }
public string g { get; set; }
public string h { get; set; }
public string i { get; set; }
public string j { get; set; }
public string k { get; set; }
public string l { get; set; }
public string m { get; set; }
public string n { get; set; }
public string o { get; set; }
public string p { get; set; }
public string q { get; set; }
public string r { get; set; }
public string s { get; set; }
public string t { get; set; }
public string u { get; set; }
public string v { get; set; }
public string w { get; set; }
public string x { get; set; }
public string y { get; set; }
public string z { get; set; }
public string Name { get; set; }
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public string E { get; set; }
public string F { get; set; }
public string G { get; set; }
public string H { get; set; }
public string I { get; set; }
public string J { get; set; }
public string K { get; set; }
public string L { get; set; }
public string M { get; set; }
public string N { get; set; }
public string O { get; set; }
public string P { get; set; }
public string Q { get; set; }
public string R { get; set; }
public string S { get; set; }
public string T { get; set; }
public string U { get; set; }
public string V { get; set; }
public string W { get; set; }
public string X { get; set; }
public string Y { get; set; }
public string Z { get; set; }
public string a { get; set; }
public string b { get; set; }
public string c { get; set; }
public string d { get; set; }
public string e { get; set; }
public string f { get; set; }
public string g { get; set; }
public string h { get; set; }
public string i { get; set; }
public string j { get; set; }
public string k { get; set; }
public string l { get; set; }
public string m { get; set; }
public string n { get; set; }
public string o { get; set; }
public string p { get; set; }
public string q { get; set; }
public string r { get; set; }
public string s { get; set; }
public string t { get; set; }
public string u { get; set; }
public string v { get; set; }
public string w { get; set; }
public string x { get; set; }
public string y { get; set; }
public string z { get; set; }
is all you need
Unicursal
Unicursal16mo ago
okay i changed that
Pobiega
Pobiega16mo ago
is it called "Name" or "ConfigName" in your JSON?
Unicursal
Unicursal16mo ago
ConfigName
Pobiega
Pobiega16mo ago
but you want the .NET class property to just be "Name", right?
Unicursal
Unicursal16mo ago
It doesnt make a difference
Pobiega
Pobiega16mo ago
either do
[JsonPropertyName("ConfigName")]
public string Name { get; set; }
[JsonPropertyName("ConfigName")]
public string Name { get; set; }
or
public string ConfigName { get; set; }
public string ConfigName { get; set; }
Unicursal
Unicursal16mo ago
i used this one then, just for more practise in changing that in .NET
Pobiega
Pobiega16mo ago
okay
Unicursal
Unicursal16mo ago
And this one? Isnt it need then too?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TextChanger
{
public class ConfigList
{
private List<Config> configs;
public List<Config> Configs { get => configs; set => configs = value; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TextChanger
{
public class ConfigList
{
private List<Config> configs;
public List<Config> Configs { get => configs; set => configs = value; }
}
}
Pobiega
Pobiega16mo ago
public class RootObject
{
public List<Config> Modes { get; set; }
}
public class RootObject
{
public List<Config> Modes { get; set; }
}
your property is called "Modes" so either use JsonPropertyName to change it, or just call it modes
Unicursal
Unicursal16mo ago
Okay changed it too
Pobiega
Pobiega16mo ago
now all you need is
string json = ""; // get a json string somehow, File.ReadAllText maybe
var obj = JsonSerializer.Deserialize<RootObject>(json);

var configs = obj.Modes;
string json = ""; // get a json string somehow, File.ReadAllText maybe
var obj = JsonSerializer.Deserialize<RootObject>(json);

var configs = obj.Modes;
Unicursal
Unicursal16mo ago
And configs is List Of Config then?
Pobiega
Pobiega16mo ago
yup
Pobiega
Pobiega16mo ago
Unicursal
Unicursal16mo ago
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?
Pobiega
Pobiega16mo ago
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 wacky
Unicursal
Unicursal16mo ago
Okay, thank you. Is there a way to use this in .NET Framework and not .NET Core?
Pobiega
Pobiega16mo ago
¯\_(ツ)_/¯ framework is dead and buried
Unicursal
Unicursal16mo ago
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"
Pobiega
Pobiega16mo ago
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 $singlefile
MODiX
MODiX16mo ago
dotnet 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:
<RuntimeIdentifier>RID</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>RID</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
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-publish
Create 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.
Accord
Accord16mo 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
More Posts