Hello everyone,
I’m a both Unity and C# newbie, so excuse me if my question is dumb.
I have a small project that i’m working on, that i picked up to learn both of the aformentioned technologies. A part of this project is retrieving all the constant values from my JSON file and using them in my game. My JSON file is fairly simple, having a following structure:
{
"heroes" : [
{
"heroNumber": 1,
"heroName": "Alex",
},
{
"heroNumber": 2,
"heroName": "Bob",
},
{
"heroNumber": 3,
"heroName": "Chris",
},
]
}
I do not intend to make the JSON file any more complex, other than, maybe, adding some more entries.
Now the code that reads the JSON file. I have two solutions for that, one being a more “complex” one and a “simpler” one.
I found the more complex one on stackexchange and it seems to work. However, i had to use a Json.NET for Unity package from jilleJr to make it work properly:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Newtonsoft.Json;
[System.Serializable]
public class HeroList : MonoBehaviour
{
void Start()
{
Deserializer.DeserializingRootItem();
}
[System.Serializable]
public partial class Deserializer
{
public static void DeserializingRootItem()
{
string jsonPath;
jsonPath = File.ReadAllText(Application.dataPath + "/list_of_heroes.json");
RootItem deserializedHero = JsonConvert.DeserializeObject<RootItem>(jsonPath);
}
}
[System.Serializable]
public class DeserializedHeroList
{
public List<RootItem> deserializedHeroList = new List<RootItem>();
}
[System.Serializable]
public partial class RootItem
{
[JsonProperty("heroes")]
public List<Hero> heroes { get; set; }
}
[System.Serializable]
public partial class Hero
{
[JsonProperty("heroNumber")]
public int heroNumber { get; set; }
[JsonProperty("heroName")]
public string heroName { get; set; }
}
}
For example: using the solution above, i’m able to deserialize the JSON and Debug.Log a value of my choosing (in this case of the second entry) by putting Debug.Log(deserializedHero.heroes[1].heroName) into the DeserializingRootItem(). However, i cannot understand how to use the deserialized data outside of that method.
I found another solution on Youtube
. I’ve tried it out as well
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JSONdeserializer : MonoBehaviour
{
public TextAsset textJSON;
[System.Serializable]
public class Hero
{
public int heroNumber;
public string heroName;
}
[System.Serializable]
public class HeroList
{
public Hero[] heroes;
}
public HeroList myHeroList = new HeroList();
// Start is called before the first frame update
void Start()
{
myHeroList = JsonUtility.FromJson<HeroList>(textJSON.text);
}
// Update is called once per frame
void Update()
{
}
}
Now, the solution above lists the heroes in the inspector windows on project’s launch. However, i cannot understand how i use the data listed.
So eventually, i have several questions:
- Which one of the two solutions would fit my simple JSON file deserialization needs better?
- In the case of the first solution, how do i use the deserialized data outside of the DeserializingRootItem()?
- In the case of the second solution, how do i use the deserialized data at all?
Thank you in advance for your answers.