Home > Net >  Using a Dictionary in a JSON-File
Using a Dictionary in a JSON-File

Time:01-11

To create the JSON file, I am using a C# Dictionary:

  public Dictionary<int, List<string>> AbsatzNrSaetzeText { get; set; } = new Dictionary<int, List<string>>();

This will result in Sound.json, which I am trying to convert - using http://json2ts.com/ - into a TypeScript data structure.

Which will result in something like this:

declare module namespace {

export interface AbsatzNrSaetzeText {
    2: string[];
    3: string[];
    4: string[];
    5: string[];
    6: string[];
    8: string[];
    9: string[];
    10: string[];
    11: string[];
//...

But this does not look very smart, because for every AbsatzNrSaetzeText or AbsatzNrSaetzeTextPunkte entry, the key (from the dictionary) is used. In the end the data structure will look pretty big, confusingly and cluttered.

Is there another solution (please provide a code snippet) to create the data structure in typescript?

CodePudding user response:

TypeScript already has a type for that Record<K, V> You can use Record<number, string[]> in this case.

  •  Tags:  
  • Related