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

How do i Read a Json with PrettyPrint?

Discussion in 'Scripting' started by Kemorno, Mar 11, 2020.

  1. Kemorno

    Kemorno

    Joined:
    Apr 5, 2017
    Posts:
    18
    So I'm using Jsons to save my classes for future use and for easier addition of new content... all was good until i tried to parse the class back into the code...

    I simply did:



    string[] jsondata = File.ReadAllLines(path);
    foreach (string s in jsondata)
    {
    Stats.Add(JsonUtility.FromJson<Stat>(s));
    }


    on my json file that looks like this:

    Code (CSharp):
    1. {
    2.     "Name": "VITALITY",
    3.     "Abreviation": "VIT",
    4.     "BaseIncrement": 0.0
    5. }
    6. {
    7.     "Name": "STRENGTH",
    8.     "Abreviation": "STR",
    9.     "BaseIncrement": 4.0
    10. }
    11. {
    12.     "Name": "DEXTERITY",
    13.     "Abreviation": "DEX",
    14.     "BaseIncrement": 0.5
    15. }
    16. {
    17.     "Name": "INTELLIGENCE",
    18.     "Abreviation": "INT",
    19.     "BaseIncrement": 0.0
    20. }
    21. {
    22.     "Name": "FAITH",
    23.     "Abreviation": "FTH",
    24.     "BaseIncrement": 0.1
    25. }
    26. {
    27.     "Name": "ARMOR",
    28.     "Abreviation": "AMR",
    29.     "BaseIncrement": 0.1
    30. }
    31.  
    But since im using file.readalllines, i'm gettin each singular line instead of the JSON class line... How should i approach it to read the whole class?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    Well, don't use ReadAllLines if you don't want your data split into lines! (There's also a ReadAllText function you can use if you'd like to get the whole file as a single string.)

    The most organized option would probably be to change your file format so that you output a single JSON object that contains all your data as subfields, and then just use a single FromJson call on the entire file.

    If you don't want to change your file format, you'll have to add some kind of logic to figure out where the object boundaries are. If your data is always going to look just like your example, the really lazy (but quite fragile) option would be just to assume every group of 5 lines is an object. If you never use child objects, you could perhaps just Split on '}' (though Split removes delimiters, so you'd have to add them back in). If you want something more robust, well, you're inching down the path towards writing your own JSON parser...
     
  3. Kemorno

    Kemorno

    Joined:
    Apr 5, 2017
    Posts:
    18
    after a couple of minutes analising the json pretty print came to the conclusion that it always ends the class with a }\r, so i made as you said and segregated each class and stored it in an array, will leave it here for anyone who needs it, a lil bit messy but i just need a base for now, will rework it later...
    THX a lot Antistone

    Code (CSharp):
    1.  
    2.             public static string[] ReadJsonPrettyPrint(string json)
    3.             {
    4.                 return GetInbetween(json, '{', "}\r", true);
    5.             }
    6.  
    7.             public static string[] GetInbetween(string content,char Openning, string Closing, bool include = true)
    8.             {
    9.                 string todo = content;
    10.                 List<string> SegregatedStrings = new List<string>();
    11.                 while (true)
    12.                 {
    13.                     int openningIndex = todo.IndexOf(Openning);
    14.                     int closingIndex = todo.IndexOf(Closing)+1;
    15.                    
    16.                     if (openningIndex == -1 || closingIndex == -1)
    17.                         break;
    18.  
    19.                     SegregatedStrings.Add(todo.Substring(openningIndex, closingIndex - openningIndex));
    20.                     todo = todo.Remove(openningIndex, closingIndex - openningIndex);
    21.  
    22.                 }
    23.                 return SegregatedStrings.ToArray();
    24.             }