Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Array of a class / class structure for Json ConfigFile

Discussion in 'Scripting' started by Kasi2302, Apr 7, 2022.

  1. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    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.


    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.IO;
    6. using UnityEngine;
    7. using Newtonsoft.Json;
    8.  
    9. public class StartConfig
    10. {
    11. public bool a = false;
    12.  
    13. public bool b = false;
    14. }
    15.  
    16.  
    17. public class German
    18. {
    19. public string name = "Deutsch";
    20.  
    21. public Vector2[] headlineVector2 = new Vector2[]
    22.     {
    23.         new Vector2(0.05f, 0.038f),
    24.         new Vector2(0.06f, 0.026f),
    25.         new Vector2(0.05f, 0.038f)
    26.     };
    27.  
    28. public ...
    29.  
    30. public ...
    31.  
    32. public ...
    33. }
    34.  
    35.  
    36. public class English
    37. {
    38.  
    39. public string name = "English";
    40.  
    41. public Vector2[] headlineVector2 = new Vector2[]
    42.     {
    43.         new Vector2(0.05f, 0.038f),
    44.         new Vector2(0.06f, 0.026f),
    45.         new Vector2(0.05f, 0.038f)
    46.     };
    47.  
    48. public ...
    49.  
    50. public ...
    51.  
    52. public ...
    53. }
    54.  
    55.  
    56. public class Languages
    57. {
    58.     public class Ger : Languages
    59.     {
    60.         public German german = new German();
    61.     }
    62.  
    63.     public class Eng : Languages
    64.     {
    65.         public English english = new English();
    66.     }
    67. }
    68.  
    69.  
    70. public class ConfigData
    71. {
    72.     public StartConfig startConfig = new StartConfig();
    73.  
    74.     public Languages[] languages = new Languages[] { new Languages.Ger(), new Languages.Eng() };
    75. }
    76.  
    77.  
    78. public class JsonConfigController : MonoBehaviour
    79. {
    80.     private string saveFile;
    81.  
    82.     public ConfigData configData = new ConfigData();
    83.    
    84.     private void Awake()
    85.     {
    86.         [B]// Here is the problem !!! The whole structure behind the array is not readable !!![/B]
    87.         Debug.Log(configData.languages[0].german.name);
    88.  
    89.         [B]// The json structure is created perfectly the way I want it !!![/B]
    90.         saveFile = Application.persistentDataPath + "/config.json";
    91. [B] [/B]
    92.  
    93.         WriteJsonConfigFile();
    94.     }
    95.  
    96.     private void WriteJsonConfigFile()
    97.     {
    98.         if (!File.Exists(saveFile))
    99.         {
    100.             JsonConvert.DefaultSettings = () => new JsonSerializerSettings
    101.             {
    102.                 Formatting = Formatting.Indented,
    103.                 ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    104.             };
    105.  
    106.             string jsonData = JsonConvert.SerializeObject(configData);
    107.  
    108.             File.WriteAllText(saveFile, jsonData);
    109.         }
    110.     }
    111. }
    112.  
     
  2. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    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?)
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    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://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    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.
     
  5. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    This is my Json struct from this code.

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


    Code (JavaScript):
    1.  
    2. {
    3.   "startConfig": {
    4.     "termsValidated": false,
    5.     "birthdayValidated": false
    6.   },
    7.   "languages": [
    8.     {
    9.       "german": {
    10.         "name": "Deutsch",
    11.         "headlineVector2": [
    12.           {
    13.             "x": 0.05,
    14.             "y": 0.038,
    15.             "normalized": {
    16.               "x": 0.796162248,
    17.               "y": 0.6050833,
    18.               "magnitude": 1,
    19.               "sqrMagnitude": 1.00000012
    20.             },
    21.             "magnitude": 0.06280127,
    22.             "sqrMagnitude": 0.00394399976
    23.           },
    24.           {
    25.             "x": 0.06,
    26.             "y": 0.026,
    27.             "normalized": {
    28.               "x": 0.91755563,
    29.               "y": 0.397607446,
    30.               "magnitude": 1,
    31.               "sqrMagnitude": 1
    32.             },
    33.             "magnitude": 0.06539113,
    34.             "sqrMagnitude": 0.004276
    35.           },
    36.           {
    37.             "x": 0.05,
    38.             "y": 0.038,
    39.             "normalized": {
    40.               "x": 0.796162248,
    41.               "y": 0.6050833,
    42.               "magnitude": 1,
    43.               "sqrMagnitude": 1.00000012
    44.             },
    45.             "magnitude": 0.06280127,
    46.             "sqrMagnitude": 0.00394399976
    47.           }
    48.         ],
    49.         "headlineFontSize": [
    50.           100,
    51.           82,
    52.           100
    53.         ],
    54.         "headlineText": [
    55.           "Bedingungen",
    56.           "In- App- Käufe",
    57.           "Hinweis"
    58.         ],
    59.         "textItemsVector2": [
    60.           {
    61.             "x": 0.075,
    62.             "y": -0.175,
    63.             "normalized": {
    64.               "x": 0.393919319,
    65.               "y": -0.919145,
    66.               "normalized": {
    67.                 "x": 0.393919349,
    68.                 "y": -0.919145048,
    69.                 "magnitude": 1,
    70.                 "sqrMagnitude": 1.00000012
    71.               },
    72.               "magnitude": 0.99999994,
    73.               "sqrMagnitude": 0.99999994
    74.             },
    75.             "magnitude": 0.190394327,
    76.             "sqrMagnitude": 0.03625
    77.           },
    78.           {
    79.             "x": 0,
    80.             "y": 0.2,
    81.             "normalized": {
    82.               "x": 0,
    83.               "y": 1,
    84.               "magnitude": 1,
    85.               "sqrMagnitude": 1
    86.             },
    87.             "magnitude": 0.2,
    88.             "sqrMagnitude": 0.0400000028
    89.           },
    90.           {
    91.             "x": 0.075,
    92.             "y": -0.21,
    93.             "normalized": {
    94.               "x": 0.336336434,
    95.               "y": -0.941741943,
    96.               "magnitude": 1,
    97.               "sqrMagnitude": 1.00000012
    98.             },
    99.             "magnitude": 0.222991019,
    100.             "sqrMagnitude": 0.0497249961
    101.           },
    102.           {
    103.             "x": 0.02,
    104.             "y": -0.35,
    105.             "normalized": {
    106.               "x": 0.0570497923,
    107.               "y": -0.998371363,
    108.               "magnitude": 1,
    109.               "sqrMagnitude": 1
    110.             },
    111.             "magnitude": 0.350570947,
    112.             "sqrMagnitude": 0.122899994
    113.           },
    114.           {
    115.             "x": 0.02,
    116.             "y": -0.34,
    117.             "normalized": {
    118.               "x": 0.05872202,
    119.               "y": -0.9982744,
    120.               "magnitude": 1,
    121.               "sqrMagnitude": 1
    122.             },
    123.             "magnitude": 0.340587735,
    124.             "sqrMagnitude": 0.116000004
    125.           }
    126.         ],
    127.         "textItemsFontSize": [
    128.           3.5,
    129.           3.2,
    130.           3.5,
    131.           3.9,
    132.           3.3
    133.         ],
    134.         "textItems": [
    135.           "Ich bestätige, dass ich mindestens <size=150%>18</size> Jahre alt bin.",
    136.           "Bitte geben Sie Ihr Geburtsdatum ein.",
    137.           "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.<br><br>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.",
    138.           "<size=150%>Merlins Magic</size> ist kostenlos zu spielen, aber Sie können den Spiel-Fortschritt mit In-App-Käufen beschleunigen.<br><br>Um dies zu deaktivieren, richten Sie den Passwortschutz für In-App-Käufe in den Einstellungen Ihres Google Play-Kontos ein.",
    139.           "<size=150%>Merlins Magic</size> ist ausschließlich für Nutzer ab <size=150%>18</size> Jahren gedacht.<br><br>Nutzer unter <size=150%>18</size> Jahren dürfen <size=150%>Merlins Magic</size> nicht spielen.<br><br>Wenn Sie <size=150%>18</size> Jahre oder älter sind, wenden Sie sich bitte an den Support, um Ihr Konto wieder freischalten zu lassen."
    140.         ],
    141.         "buttonTextFontSize": [
    142.           30,
    143.           30,
    144.           110
    145.         ],
    146.         "buttonText": [
    147.           "Bestätigen",
    148.           "Support  kontaktieren",
    149.           "Gast"
    150.         ]
    151.       }
    152.     },
    153.     {
    154.       "english": {
    155.         "name": "English",
    156.         "headlineVector2": [
    157.           {
    158.             "x": 0.05,
    159.             "y": 0.038,
    160.             "normalized": {
    161.               "x": 0.796162248,
    162.               "y": 0.6050833,
    163.               "magnitude": 1,
    164.               "sqrMagnitude": 1.00000012
    165.             },
    166.             "magnitude": 0.06280127,
    167.             "sqrMagnitude": 0.00394399976
    168.           },
    169.           {
    170.             "x": 0.06,
    171.             "y": 0.026,
    172.             "normalized": {
    173.               "x": 0.91755563,
    174.               "y": 0.397607446,
    175.               "magnitude": 1,
    176.               "sqrMagnitude": 1
    177.             },
    178.             "magnitude": 0.06539113,
    179.             "sqrMagnitude": 0.004276
    180.           },
    181.           {
    182.             "x": 0.05,
    183.             "y": 0.038,
    184.             "normalized": {
    185.               "x": 0.796162248,
    186.               "y": 0.6050833,
    187.               "magnitude": 1,
    188.               "sqrMagnitude": 1.00000012
    189.             },
    190.             "magnitude": 0.06280127,
    191.             "sqrMagnitude": 0.00394399976
    192.           }
    193.         ],
    194.         "headlineFontSize": [
    195.           100,
    196.           82,
    197.           100
    198.         ],
    199.         "headlineText": [
    200.           "Conditions",
    201.           "In- App- Purchases",
    202.           "Notice"
    203.         ],
    204.         "textItemsVector2": [
    205.           {
    206.             "x": 0.075,
    207.             "y": -0.175,
    208.             "normalized": {
    209.               "x": 0.393919319,
    210.               "y": -0.919145,
    211.               "normalized": {
    212.                 "x": 0.393919349,
    213.                 "y": -0.919145048,
    214.                 "magnitude": 1,
    215.                 "sqrMagnitude": 1.00000012
    216.               },
    217.               "magnitude": 0.99999994,
    218.               "sqrMagnitude": 0.99999994
    219.             },
    220.             "magnitude": 0.190394327,
    221.             "sqrMagnitude": 0.03625
    222.           },
    223.           {
    224.             "x": 0,
    225.             "y": 0.2,
    226.             "normalized": {
    227.               "x": 0,
    228.               "y": 1,
    229.               "magnitude": 1,
    230.               "sqrMagnitude": 1
    231.             },
    232.             "magnitude": 0.2,
    233.             "sqrMagnitude": 0.0400000028
    234.           },
    235.           {
    236.             "x": 0.075,
    237.             "y": -0.21,
    238.             "normalized": {
    239.               "x": 0.336336434,
    240.               "y": -0.941741943,
    241.               "magnitude": 1,
    242.               "sqrMagnitude": 1.00000012
    243.             },
    244.             "magnitude": 0.222991019,
    245.             "sqrMagnitude": 0.0497249961
    246.           },
    247.           {
    248.             "x": 0.02,
    249.             "y": -0.35,
    250.             "normalized": {
    251.               "x": 0.0570497923,
    252.               "y": -0.998371363,
    253.               "magnitude": 1,
    254.               "sqrMagnitude": 1
    255.             },
    256.             "magnitude": 0.350570947,
    257.             "sqrMagnitude": 0.122899994
    258.           },
    259.           {
    260.             "x": 0.02,
    261.             "y": -0.34,
    262.             "normalized": {
    263.               "x": 0.05872202,
    264.               "y": -0.9982744,
    265.               "magnitude": 1,
    266.               "sqrMagnitude": 1
    267.             },
    268.             "magnitude": 0.340587735,
    269.             "sqrMagnitude": 0.116000004
    270.           }
    271.         ],
    272.         "textItemsFontSize": [
    273.           3.5,
    274.           3.2,
    275.           3.5,
    276.           3.9,
    277.           3.3
    278.         ],
    279.         "textItems": [
    280.           "I confirm that I am at least <size=150%>18</size> years old.",
    281.           "Please enter your date of birth.",
    282.           "I confirm that I have read, understood and agree to the <link=\"https://mystery-soft.de/\"><u>Terms and Conditions</u></link>.<br><br>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>.",
    283.           "<size=150%>Merlins Magic</size> is free to play, but you can speed up the game progress with In-App-Purchases.<br><br>To disable this, set up password protection for In-App-Purchases in your Google Play account settings.",
    284.           "<size=150%>Merlins Magic</size> is intended exclusively for users over the age of <size=150%>18</size>.<br><br>Users under the age of <size=150%>18</size> are not allowed to play <size=150%>Merlins Magic</size>.<br><br>If you are <size=150%>18</size> or older, please contact support to have your account unlocked."
    285.         ],
    286.         "buttonTextFontSize": [
    287.           30,
    288.           30,
    289.           110
    290.         ],
    291.         "buttonText": [
    292.           "Confirm",
    293.           "Contact  Support",
    294.           "Guest"
    295.         ]
    296.       }
    297.     }
    298.   ]
    299. }
    300.  
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    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.
     
  7. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    Thank you very much for your answer.

    I hope someone can help me quickly. o_O
     
  8. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25

    WOW :rolleyes: What a wonderful script (JSON2CSharp) :)

    I will test it tomorrow. :)

    Thank you very much. :)
     
  9. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    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.