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
1
Question by NebulaNu · Nov 23, 2019 at 08:43 AM · jsonlistssaving

Saving a list to json

Hey all, I'm having trouble saving out to a json file. I built up a small mock program to show what I'm attempting to do.

 public class Contoller : MonoBehaviour
 {
     private string installPath;
     private bool directoryExists;
 
     private string jsonString;
 
     private Store lots_o_stuff = new Store ();
     private Product food = new Product ();
     private Product cloths = new Product ();
 
     void Start ()
     {
         installPath = Path.GetDirectoryName (Application.dataPath);
         checkForDirectory ();
         checkForStore ();
         Debug.Log (lots_o_stuff.product [0].item_Name [0]);
     }
 
     private void checkForDirectory ()
     {
         try {
             if (!Directory.Exists (installPath + "/Configs")) {
                 Directory.CreateDirectory (installPath + "/Configs");
                 directoryExists = true;
             } else {
                 directoryExists = true;
             }
         } catch {
             Debug.Log ("Failed Directory");
         }
     }
 
     private void checkForStore ()
     {
         if (directoryExists) {
             try {
                 if (!File.Exists (installPath + "/Configs/Store.json")) {
                     buildDefaultStore ();
                     jsonString = JsonUtility.ToJson (lots_o_stuff, true);
                     File.WriteAllText (installPath + "/Configs/Store.json", jsonString);
                 }
             } catch {
                 Debug.Log ("Failed Store");
             }
         }
     }
 
     private void buildDefaultStore ()
     {
         lots_o_stuff.store_Name = "Lots O Stuff";
         lots_o_stuff.store_Address = "123 Adress St.";
         string[] food_Items = new string[] { "Banana", "Apple", "Shrimp", "Ice Cream" };
         string[] cloth_Items = new string[] { "Shirt", "Pants", "Belt", "Boots" };
         food.Type = "Food";
         food.item_Name.AddRange (food_Items);
         cloths.Type = "Cloths";
         cloths.item_Name.AddRange (cloth_Items);
         lots_o_stuff.product.Add (food);
         lots_o_stuff.product.Add (cloths);
     }
 }
 
 public class Store
 {
     public string store_Name;
     public string store_Address;
     public List<Product> product = new List<Product> ();
 }
 
 public class Product
 {
     public string Type;
     public List<string> item_Name = new List<string> ();
 }

When I go to save out, this is all that gets saved.

 {
     "store_Name": "Lots O Stuff",
     "store_Address": "123 Adress St."
 }

The list "Product" doesn't even show up empty.

Does anyone know a way to make this work?

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

1 Reply

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

Answer by Bunny83 · Nov 23, 2019 at 11:53 AM

You haven't marked any of your classes as [System.Serializable]

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 NebulaNu · Nov 24, 2019 at 01:44 AM 0
Share

Thank you very much!

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

119 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 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 file not written/read on application exit on android 1 Answer

Instancing ScriptableObject at Runtime, loading and saving via JsonUtility 1 Answer

Storing list aka "save function" 3 Answers

How to save and load mobs? 0 Answers

Serializing lists 0 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