Home > Blockchain >  Json config fill in C#
Json config fill in C#

Time:03-07

Im trying to create a json editor. Im trying to read the json file and convert this data to editble data.

In the json file there are sections, subsections and always 9 value. This is how all json files will look like. But what the names of the sections and subsections are, is always different. (So its possible there is 10 sections, with 100 subsections).

Im trying to load those settings threw a class.

public class JsonValues
{
    public string? Section { get; set; }
    public string? Value { get; set; }
    public string? Type { get; set; }
    public int VisibleTo { get; set; }
    public int EditableFor { get; set; }
    public int Maxlength { get; set; }
    public int Min { get; set; }
    public int Max { get; set; }
    public string[]? ListItems { get; set; }
    public string? Description { get; set; }
}

The json file looks like:
{
  "General": {
    "Language": {
      "Value": 1,
      "Type": "Int",
      "VisibleTo": 2,
      "EditableFor": 2,
      "MaxLength": 0,
      "Min": 0,
      "Max": 9,
      "ListItems": "",
      "Description": "Default global program language (0::Build-In, others depend on LTX file)"
    },
    "Licensed": {
      "Value": "ItsME",
      "Type": "String",
      "VisibleTo": "3",
      "EditableFor": "4",
      "MaxLength": "50",
      "Min": "0",
      "Max": "0",
      "ListItems": "",
      "Description": "License Text"
    },
    "Program Code": {
      "Value": "55",
      "Type": "String",
      "VisibleTo": "2",
      "EditableFor": "4",
      "MaxLength": "25",
      "Min": "0",
      "Max": "0",
      "ListItems": "",
      "Description": "Program identification code"
    }
  },
  "EditMe": {
    "FileName": {
      "Value": "Cfg/EditMe.json",
      "Type": "String",
      "VisibleTo": "4",
      "EditableFor": "5",
      "MaxLength": "0",
      "Min": "0",
      "Max": "0",
      "ListItems": "",
      "Description": "Folder were to find the localised Text Definition Files"
    }
  }
}

I was trying something like:

var jsonString = File.ReadAllText(jsonLocation);
JsonValues JsonValues = System.Text.Json.JsonSerializer.Deserialize<JsonValues>(jsonString);

But I didnt get it working yet....

any suggestions...

CodePudding user response:

you have to try to use Newtonsoft.Json since you need a serious json library for your json utility

using Newtonsoft.Json;

    Data JsonValues = JsonConvert.DeserializeObject<Data>(json);

classes

public class Data
{
    public General General { get; set; }
    public EditMe EditMe { get; set; }
}

public class General
{
    public JsonValues Language { get; set; }
    public JsonValues Licensed { get; set; }

    [JsonProperty("Program Code")]
    public JsonValues ProgramCode { get; set; }
}
public class EditMe
{
    public JsonValues FileName { get; set; }
}

CodePudding user response:

You can use the following Model structure to hold your JSON data:

public class JsonValues
{
    public string Value { get; set; }
    public string Type { get; set; }
    public string VisibleTo { get; set; }
    public string EditableFor { get; set; }
    public string MaxLength { get; set; }
    public string Min { get; set; }
    public string Max { get; set; }
    public string ListItems { get; set; }
    public string Description { get; set; }
}

And you can deserialize with dynamic nodes to your Model class like this:

using System.Text.Json;

string jsonString = File.ReadAllText(jsonLocation);
var result = JsonSerializer.Deserialize<Dictionary<string,Dictionary<string,JsonValues>>>(jsonString);

foreach(var item in result)
{
    foreach(var item1 in item.Value)
    {
        Console.WriteLine(item1.Value.EditableFor);             
    }

}

Output:

2
4
4
5
  • Related