Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How to Load The Json File From TextAsset

Discussion in 'Scripting' started by Alexander21, Jun 24, 2017.

  1. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    242
    Hi All

    I am new bie to unity . This is my code for loading the json file into unity.
    Code (CSharp):
    1. public class Save
    2.     {
    3.  
    4.         public string[] First= new string[5];
    5.  
    6.     };
    7.  
    8.     public class Player33
    9.     {
    10.  
    11.         //    public string playerId;
    12.         //    public string playerLoc;
    13.         //    public string playerNick;
    14.         public string First;
    15.         public string Second;
    16.         public string Third;
    17.         public string Fourth;
    18.  
    19.  
    20.     }
    21.  
    22.  
    23. public void Save2()
    24.     {
    25.         Save saveData = new Save();
    26.     string jsonString = "{\"First\":\"LeaderBoard\",\"Second\":\"Transition\",\"Third\":\"Nutrition\",\"Fourth\":\"LevelMap\"}";
    27.  
    28.         Player33 player = JsonUtility.FromJson<Player33>(jsonString);
    29. saveData.First[0] =player.First;
    30.         saveData.First[1]=player.Second ;
    31.         saveData.First[2]=player.Third;
    32.         saveData.First[3]=player.Fourth;
    33.  
    34.         Debug.Log("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    35.         Debug.Log(saveData.First[0]);
    36.         Debug.Log(saveData.First[1]);
    37.         Debug.Log(saveData.First[2]);
    38.         Debug.Log(saveData.First[3]);
    39.         //Debug.Log(player.Fourth);
    40.         string jsonData = JsonUtility.ToJson(saveData);
    41.         //Save Json string
    42.         PlayerPrefs.SetString("MySettings", jsonData);
    43.         PlayerPrefs.Save();
    44.     }
    45.  

    The code is fine and good. My problem is i dont know how to load the json file Dynamically. I have Save the

    json file in the disk

    Code (CSharp):
    1.  
    2. {\"First\":\"Level\",\"Level2\":\"Start\",\"Third\":\"Level3\",\"Fourth\":\"Level4\"}";
    3.  
    4. i have assigned public TextAsset jsonfileload;
    5.  
    6.     Player33 player = JsonUtility.FromJson<Player33>(jsonfileload);
    7. But it shows error. How can i load from the json file dyanmically.
    8.  
    9. and also i have tryed
    10. string path = "Assets/Resources/flowstring.json";
    11.  
    12. /    StreamReader reader = new StreamReader(path);
    13.  
    14. string k =reader;
    15.  
    16.   Player33 player = JsonUtility.FromJson<Player33>(k);
    17.  
    Its also shows error. How can i load from the resource file dyanmically...

    Thanks in Advance...
     
    masterless123 likes this.
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,509
    First your class needs to be serializable as you see here https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html

    Next, you want the text from your textasset, so you need to use jsonfileload.text or jsonfileload.ToString().

    Now, to load from a resource folder you should use

    TextAsset myTextAsset = Resources.Load("flowstring") as TextAsset; Do not include the file extension, just the name of the file.

    Also note that json files do not need escapes on the ". So \" is not needed when loading the json from a file, and actually would create errors (if you have those in your json file)
     
    pocoinik likes this.
  3. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    242
    Thanks Brathnan for you reply. I have followed your steps...... and i have solved it. thanks for your help.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,509
    Glad you got it to work!
     
  5. hertz-rat

    hertz-rat

    Joined:
    Nov 3, 2019
    Posts:
    71
    Strongly typed languages are so awful to work with. I've spent all day writing unnecessary classes and serializing things when all I'm trying to do is pass around data. All of this would have taken an hour in nodejs
     
    GoldRobert likes this.
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,509
    Why did you necro a post to make a pointless comment?

    And just because it takes you all day doesn't mean it does for other people who could just as easily be doing things in an hour.
     
  7. hertz-rat

    hertz-rat

    Joined:
    Nov 3, 2019
    Posts:
    71
    Serialization is supposed to be incredibly simple, but in C# (or is it Unity?) its very limited. You can't de/serialize a property that is readonly, generic, private, or even nested. You can't deserialize a simple top-level array, and it can't handle dictionaries, despite json basically being a dictionary structure. The third party libraries are all quirky with weird documenation - they seem to work once you figure out their quirks though. Then again, people in the forums say that some of them break compatibility with some platforms (eg, ios). There are some more details here [1], see donHopkins' posts near the bottom.

    The comment isn't pointless: Unity as a company should use some of their budget to do create a better module for this. This serialization stuff is the hardest thing I've had to do in the whole 4 week process of making my game so far, including learning Unity itself, building a server, adding pathfinding, a turn based system, and animating. In other languages, serialization is a one-liner that you don't even have to think about.

    https://news.ycombinator.com/item?id=13197461

    edited to elaborate
     
    Last edited: Jan 30, 2020
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,509
    C# json deserialize/serialize works just fine. Never had an issue. Again, just because you're having trouble doesn't mean others have. It's just one line to do either. Unity's version is a bit more strict in what it supports, but I've used json.net for a long time and it's simple to use and have never had an issue on any platform.
     
  9. ganiyusikiru

    ganiyusikiru

    Joined:
    Jan 12, 2020
    Posts:
    3
    Hi, I made a building in Autodesk Revit which I import into Unity using Twinmotion. Unfortunately, the model data/parameters were not fully imported into Unity. I needed to import the data to Unity in JSON format. I have extracted the data in JSON format from Revit using Dynamo to Excel. (Dynamo - Excel - CSV - JSON)
    My challenge now is how to import the JSON file into Unity and attach it to my Model.
    Any help on how to go about this? I am a starter.
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,509
    Make your own post. What you're asking for doesn't sound like loading data to use at runtime, but something with importing json data to apply to a model.
     
unityunity