Read one value from a json file using System.json.text

  • Thread starter Thread starter HansvB69
  • Start date Start date
H

HansvB69

Guest
Hi

Until now i used Newtonsoft.Json to read and write a json file. Now i want to use System.Text.Json but i am not able to read a single value from the json file.

So far i have this:

class SettingsManager
{
public static JsonDocument doc;

public SettingsManager()
{
SettingsFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AppSettingsDefault.ApplicationName, AppSettingsDefault.SettingsFolder, AppSettingsDefault.ConfigFile); //...\appdata\roaming\<application>\settings\...
}

public class AppSettingsMeta
{
public List<AppParams> AppParam { get; set; }
}

public class AppParams
{
public bool ActivateLogging { get; set; }
public bool AppendLogFile { get; set; }

public string ApplicationName { get; set; }
public string ApplicationVersion { get; set; }
public string ApplicationBuildDate { get; set; }
}


public void LoadSettings()
{
if (File.Exists(SettingsFile))
{
Json = File.ReadAllText(SettingsFile);
//jsonObjSettings = JsonSerializer.Deserialize<AppSettingsMeta>(Json);

doc = JsonDocument.Parse(Json);
}
}
}



I the main form i want to read the "AppendLogFile" from the json



JsonElement root = SettingsManager.doc.RootElement;
var u1 = root[0]; --> error : ex = {"The requested operation requires an element of type 'Array', but the target element has type 'Object'."}

How can i read one value out of a json file?

Greatings

Continue reading...
 
Back
Top