- Home /
Load all Json Files from Resources into a converted list?
Hi guys,
I know this code isn't right, but i'm not sure how to go about it. How do I load all of the Json files from my 'items' folder in resources and convert the data to a list? Is this an efficient way to do this (using Resource.LoadAll) ? Your help would be much appreciated.
This is the code I have at the moment:
public class JsonReader : MonoBehaviour {
public List<Item> items;
void Start () {
LoadJsonAsResource("Items");
}
public Object[] loadedJsonFiles;
public void LoadJsonAsResource(string path)
{
loadedJsonFiles = Resources.LoadAll(path, typeof(TextAsset));
foreach (Object asset in loadedJsonFiles)
{
Item myItem = JsonUtility.FromJson<Object>(asset);
items.Add (myItem);
}
}
}
Answer by juniperspark · Nov 11, 2017 at 08:43 PM
Hey guys,
Thanks for your messages and trying to help. I was looking for a set up to allow me to have a json file per item rather than a json file with everything in it.
What i have found that works is below. If anyone has any other ideas on how i could do this, let me know. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class JsonReader : MonoBehaviour
{
public List<Item> items;
public List<string> dictionaryPaths;
void Start ()
{
LoadJsonAsResource("Items");
}
public void LoadJsonAsResource(string path)
{
// Loads all TextAssets into a list.
Object[] jsonFileArray = Resources.LoadAll(path, typeof(TextAsset));
// Adds the paths to the paths dictionary.
foreach (Object asset in jsonFileArray)
{
string assetPath = AssetDatabase.GetAssetPath(asset);
assetPath = assetPath.Replace("Assets/Resources/", "");
dictionaryPaths.Add(assetPath);
}
foreach (string dictionaryItem in dictionaryPaths)
{
LoadItem (dictionaryItem);
}
}
public void LoadItem (string path)
{
string myLoadedItem = LoadJson(path);
Item myItem = JsonUtility.FromJson<Item>(myLoadedItem);
items.Add (myItem);
}
public static string LoadJson(string path)
{
string jsonFilePath = path.Replace(".json", "");
TextAsset loadedJsonFile = Resources.Load<TextAsset>(jsonFilePath);
return loadedJsonFile.text;
Debug.Log ("Reading Text File: " + jsonFilePath);
}
}
Answer by Bunny83 · Nov 09, 2017 at 10:05 PM
Your code doesn't make much sense. First of all FromJson expects a string as parameter, not a TextAsset. You should pass asset.text
.
Second you have to specify the actual type you want to deserialize. The JSON representation is a pute data representation. It doesn't contain a certain datatype. You passed "Object" as generic type which doesn't make much sense as Object doesn't have any fields that could be populated from the JSON data.
What do those JSON files actually contain? Do you have an example?
Answer by EDevJogos · Nov 09, 2017 at 10:09 PM
My suggestion is:
Put your item List into a class, like ItemsManager your something like that.
Than just create your items in the JSon file in a List format like:
{"items":[{"id":0,"name":"ItemName0"},{"id":0,"name":"ItemName1"}]}
As you can see in the string above items is the List name and each object in this list is an item, they were made to fit with this classes bellow:
[Serializable]
public class Item
{
public int id;
public string name;
}
[Serializable]
public class ItemWarpper
{
public List<Item> items = new List<Item>();
}
public ItemWarpper myItems = new ItemWarpper();
Than to load this Json just call:
public TextAsset itemsTeste;
private void Start()
{
myItems = JsonUtility.FromJson<CloudData.ItemWarpper>(itemsTeste.text);
}
Make sure that your classes have [Serializable] and that you have using System; on your .cs file.
Forgot to mention this class Item and ItemWarpper are in a class .cs CloudData, it was open on visual studio when i was making this example, so i used it XD.