Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by juniperspark · Nov 09, 2017 at 09:29 PM · jsonlistsresources.loadall

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);
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EDevJogos · Nov 09, 2017 at 10:12 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

74 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JSON into Unity Class 4 Answers

What is the most performant way to handle huge List containing data to frame-sync Objects to 360 video? 0 Answers

How to rapidly assign and reassign component data within gameobjects based off API data 0 Answers

Saving a list to json 1 Answer

Instantiate random prefab from folder using Resources.LoadAll 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges