Array of a class / class structure for Json ConfigFile

Hello, I hope someone can help me please, please, I’ve been sitting on this for a few days and just can’t find the solution.

I managed to create my json structure as I imagine it , but I can’t access a value after the array.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Newtonsoft.Json;

public class StartConfig
{
public bool a = false;

public bool b = false;
}


public class German
{
public string name = "Deutsch";

public Vector2[] headlineVector2 = new Vector2[]
    {
        new Vector2(0.05f, 0.038f),
        new Vector2(0.06f, 0.026f),
        new Vector2(0.05f, 0.038f)
    };

public ...

public ...

public ...
}


public class English
{

public string name = "English";

public Vector2[] headlineVector2 = new Vector2[]
    {
        new Vector2(0.05f, 0.038f),
        new Vector2(0.06f, 0.026f),
        new Vector2(0.05f, 0.038f)
    };

public ...

public ...

public ...
}


public class Languages
{
    public class Ger : Languages
    {
        public German german = new German();
    }

    public class Eng : Languages
    {
        public English english = new English();
    }
}


public class ConfigData
{
    public StartConfig startConfig = new StartConfig();

    public Languages[] languages = new Languages[] { new Languages.Ger(), new Languages.Eng() };
}


public class JsonConfigController : MonoBehaviour
{
    private string saveFile;

    public ConfigData configData = new ConfigData();
   
    private void Awake()
    {
        [B]// Here is the problem !!! The whole structure behind the array is not readable !!![/B]
        Debug.Log(configData.languages[0].german.name);

        [B]// The json structure is created perfectly the way I want it !!![/B]
        saveFile = Application.persistentDataPath + "/config.json";
[B] [/B]

        WriteJsonConfigFile();
    }

    private void WriteJsonConfigFile()
    {
        if (!File.Exists(saveFile))
        {
            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                Formatting = Formatting.Indented,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            };

            string jsonData = JsonConvert.SerializeObject(configData);

            File.WriteAllText(saveFile, jsonData);
        }
    }
}

This error message comes:

Assets\Scripts\JsonConfigController.cs(87,43): error CS1061: ‘Languages’ does not contain a definition for ‘german’ and no accessible extension method ‘german’ accepting a first argument of type ‘Languages’ could be found (are you missing a using directive or an assembly reference?)

Pull it all apart, just copy the code out and do it line by line by line without all the above complication:

  • load the JSON
  • display it with Debug.Log()
  • parse the JSON into objects
  • get at the data piece by piece

Also, are you sure you shaped your structures properly?

Be sure to leverage sites like:

https://csharp2json.io

Thinking about this more, are you expecting that loading some JSON having a field called “german” will make an actual variable called .german? Because that does not happen in C#. That’s a Javascript thing. C# is compiled and typechecked in advance.

This is my Json struct from this code.

I need the structure just so, it is perfect for what I have in mind.

{
  "startConfig": {
    "termsValidated": false,
    "birthdayValidated": false
  },
  "languages": [
    {
      "german": {
        "name": "Deutsch",
        "headlineVector2": [
          {
            "x": 0.05,
            "y": 0.038,
            "normalized": {
              "x": 0.796162248,
              "y": 0.6050833,
              "magnitude": 1,
              "sqrMagnitude": 1.00000012
            },
            "magnitude": 0.06280127,
            "sqrMagnitude": 0.00394399976
          },
          {
            "x": 0.06,
            "y": 0.026,
            "normalized": {
              "x": 0.91755563,
              "y": 0.397607446,
              "magnitude": 1,
              "sqrMagnitude": 1
            },
            "magnitude": 0.06539113,
            "sqrMagnitude": 0.004276
          },
          {
            "x": 0.05,
            "y": 0.038,
            "normalized": {
              "x": 0.796162248,
              "y": 0.6050833,
              "magnitude": 1,
              "sqrMagnitude": 1.00000012
            },
            "magnitude": 0.06280127,
            "sqrMagnitude": 0.00394399976
          }
        ],
        "headlineFontSize": [
          100,
          82,
          100
        ],
        "headlineText": [
          "Bedingungen",
          "In- App- Käufe",
          "Hinweis"
        ],
        "textItemsVector2": [
          {
            "x": 0.075,
            "y": -0.175,
            "normalized": {
              "x": 0.393919319,
              "y": -0.919145,
              "normalized": {
                "x": 0.393919349,
                "y": -0.919145048,
                "magnitude": 1,
                "sqrMagnitude": 1.00000012
              },
              "magnitude": 0.99999994,
              "sqrMagnitude": 0.99999994
            },
            "magnitude": 0.190394327,
            "sqrMagnitude": 0.03625
          },
          {
            "x": 0,
            "y": 0.2,
            "normalized": {
              "x": 0,
              "y": 1,
              "magnitude": 1,
              "sqrMagnitude": 1
            },
            "magnitude": 0.2,
            "sqrMagnitude": 0.0400000028
          },
          {
            "x": 0.075,
            "y": -0.21,
            "normalized": {
              "x": 0.336336434,
              "y": -0.941741943,
              "magnitude": 1,
              "sqrMagnitude": 1.00000012
            },
            "magnitude": 0.222991019,
            "sqrMagnitude": 0.0497249961
          },
          {
            "x": 0.02,
            "y": -0.35,
            "normalized": {
              "x": 0.0570497923,
              "y": -0.998371363,
              "magnitude": 1,
              "sqrMagnitude": 1
            },
            "magnitude": 0.350570947,
            "sqrMagnitude": 0.122899994
          },
          {
            "x": 0.02,
            "y": -0.34,
            "normalized": {
              "x": 0.05872202,
              "y": -0.9982744,
              "magnitude": 1,
              "sqrMagnitude": 1
            },
            "magnitude": 0.340587735,
            "sqrMagnitude": 0.116000004
          }
        ],
        "textItemsFontSize": [
          3.5,
          3.2,
          3.5,
          3.9,
          3.3
        ],
        "textItems": [
          "Ich bestätige, dass ich mindestens <size=150%>18</size> Jahre alt bin.",
          "Bitte geben Sie Ihr Geburtsdatum ein.",
          "Ich bestätige, dass ich die <link=\"https://mystery-soft.de/\"><u>Allgemeinen Geschäftsbedingungen</u></link> gelesen und verstanden habe und stimme ihnen zu.

Indem ich fortfahre, erkläre ich mich damit einverstanden, dass <link=\"https://mystery-soft.de/\"><u>Mystery Soft</u></link> meine Daten in Übereinstimmung mit den <link=\"https://mystery-soft.de/\"><u>Datenschutzrichtlinien</u></link> speichern und verarbeiten darf.",
          "<size=150%>Merlins Magic</size> ist kostenlos zu spielen, aber Sie können den Spiel-Fortschritt mit In-App-Käufen beschleunigen.

Um dies zu deaktivieren, richten Sie den Passwortschutz für In-App-Käufe in den Einstellungen Ihres Google Play-Kontos ein.",
          "<size=150%>Merlins Magic</size> ist ausschließlich für Nutzer ab <size=150%>18</size> Jahren gedacht.

Nutzer unter <size=150%>18</size> Jahren dürfen <size=150%>Merlins Magic</size> nicht spielen.

Wenn Sie <size=150%>18</size> Jahre oder älter sind, wenden Sie sich bitte an den Support, um Ihr Konto wieder freischalten zu lassen."
        ],
        "buttonTextFontSize": [
          30,
          30,
          110
        ],
        "buttonText": [
          "Bestätigen",
          "Support  kontaktieren",
          "Gast"
        ]
      }
    },
    {
      "english": {
        "name": "English",
        "headlineVector2": [
          {
            "x": 0.05,
            "y": 0.038,
            "normalized": {
              "x": 0.796162248,
              "y": 0.6050833,
              "magnitude": 1,
              "sqrMagnitude": 1.00000012
            },
            "magnitude": 0.06280127,
            "sqrMagnitude": 0.00394399976
          },
          {
            "x": 0.06,
            "y": 0.026,
            "normalized": {
              "x": 0.91755563,
              "y": 0.397607446,
              "magnitude": 1,
              "sqrMagnitude": 1
            },
            "magnitude": 0.06539113,
            "sqrMagnitude": 0.004276
          },
          {
            "x": 0.05,
            "y": 0.038,
            "normalized": {
              "x": 0.796162248,
              "y": 0.6050833,
              "magnitude": 1,
              "sqrMagnitude": 1.00000012
            },
            "magnitude": 0.06280127,
            "sqrMagnitude": 0.00394399976
          }
        ],
        "headlineFontSize": [
          100,
          82,
          100
        ],
        "headlineText": [
          "Conditions",
          "In- App- Purchases",
          "Notice"
        ],
        "textItemsVector2": [
          {
            "x": 0.075,
            "y": -0.175,
            "normalized": {
              "x": 0.393919319,
              "y": -0.919145,
              "normalized": {
                "x": 0.393919349,
                "y": -0.919145048,
                "magnitude": 1,
                "sqrMagnitude": 1.00000012
              },
              "magnitude": 0.99999994,
              "sqrMagnitude": 0.99999994
            },
            "magnitude": 0.190394327,
            "sqrMagnitude": 0.03625
          },
          {
            "x": 0,
            "y": 0.2,
            "normalized": {
              "x": 0,
              "y": 1,
              "magnitude": 1,
              "sqrMagnitude": 1
            },
            "magnitude": 0.2,
            "sqrMagnitude": 0.0400000028
          },
          {
            "x": 0.075,
            "y": -0.21,
            "normalized": {
              "x": 0.336336434,
              "y": -0.941741943,
              "magnitude": 1,
              "sqrMagnitude": 1.00000012
            },
            "magnitude": 0.222991019,
            "sqrMagnitude": 0.0497249961
          },
          {
            "x": 0.02,
            "y": -0.35,
            "normalized": {
              "x": 0.0570497923,
              "y": -0.998371363,
              "magnitude": 1,
              "sqrMagnitude": 1
            },
            "magnitude": 0.350570947,
            "sqrMagnitude": 0.122899994
          },
          {
            "x": 0.02,
            "y": -0.34,
            "normalized": {
              "x": 0.05872202,
              "y": -0.9982744,
              "magnitude": 1,
              "sqrMagnitude": 1
            },
            "magnitude": 0.340587735,
            "sqrMagnitude": 0.116000004
          }
        ],
        "textItemsFontSize": [
          3.5,
          3.2,
          3.5,
          3.9,
          3.3
        ],
        "textItems": [
          "I confirm that I am at least <size=150%>18</size> years old.",
          "Please enter your date of birth.",
          "I confirm that I have read, understood and agree to the <link=\"https://mystery-soft.de/\"><u>Terms and Conditions</u></link>.

By continuing, I agree that <link=\"https://mystery-soft.de/\"><u>Mystery Soft</u></link> may store and process my data in accordance with the <link=\"https://mystery-soft.de/\"><u>Privacy Policy</u></link>.",
          "<size=150%>Merlins Magic</size> is free to play, but you can speed up the game progress with In-App-Purchases.

To disable this, set up password protection for In-App-Purchases in your Google Play account settings.",
          "<size=150%>Merlins Magic</size> is intended exclusively for users over the age of <size=150%>18</size>.

Users under the age of <size=150%>18</size> are not allowed to play <size=150%>Merlins Magic</size>.

If you are <size=150%>18</size> or older, please contact support to have your account unlocked."
        ],
        "buttonTextFontSize": [
          30,
          30,
          110
        ],
        "buttonText": [
          "Confirm",
          "Contact  Support",
          "Guest"
        ]
      }
    }
  ]
}

Excellent… in Javascript your code will work.

In C# the word “german” will absolutely NEVER produce a compiler-checked variable called .german

Go up to my post and look at JSON2CSharp link… feed your JSON into that and you will get back a class that is shaped nearly-perfectly, then you can twiddle it a bit more by hand and deserialize to that.

Thank you very much for your answer.

I hope someone can help me quickly. :eyes:

WOW :roll_eyes: What a wonderful script (JSON2CSharp) :slight_smile:

I will test it tomorrow. :slight_smile:

Thank you very much. :slight_smile:

Your trying to access a field called german in the class Languages, but there is no such field. The only thing in the class Languages are Ger and Eng. What you’re trying to access, and your code structure, are two different things currently.