Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NODUF · Jan 09 at 06:53 PM · listsaveloadjson

Saving/Loading class list with SimpleJSON,Saving class list with SimpleJSON

Hi, I don't know what's wrong. On save it saves it but on load it loads it only in string.

class list:

 public class BuildingData
 
 {
     public int placeID;
     public float upgradePrice;
     public int upgradeLevel;
     public int happinessValue;
     public float peoplePerSec;
     public string buildingName;
     public int buildingPrice;
     public int buildingLevel;
     public float buildingProduction;
     public int buildingID;
 }

code:

 void Start(){
     Load<BuildingData>("key1");
 }

 public void AutoSave(){
     Save<BuildingData>("key1", buildingData);
 }

 public static void Save<BuildingData>(string key, List<BuildingData> objs){
     JSONArray jsonArray = new JSONArray();
     foreach(BuildingData obj in objs){
         try{
             string jsonObjString = JsonUtility.ToJson(obj);
             jsonArray.Add(JSON.Parse(jsonObjString));
             Debug.Log(objs);
         }
         catch(Exception e){

         }
     }
     PlayerPrefs.SetString(key, jsonArray.ToString());
     Debug.Log("Saved" + jsonArray.ToString());
 }

 public static List<BuildingData> Load<BuildingData>(string key){
     List<BuildingData> objs = new List<BuildingData>();

     string jsonString = PlayerPrefs.GetString(key, "[]");
     Debug.Log("LOAD String " + jsonString);
     try{
         JSONArray jsonArr = (JSONArray)JSON.Parse(jsonString);
         Debug.Log("LOAD Array " + jsonArr);
         foreach(JSONObject jsonObj in jsonArr){
             objs.Add(JsonUtility.FromJson<BuildingData>(jsonObj.ToString()));
             Debug.Log("LOAD Object " + jsonObj);
         }
     }
     catch(Exception e){

     }
     return objs;
 }

}

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
0

Answer by Bunny83 · Jan 10 at 01:17 AM

Well your usage of my SimpleJSON library is quite strange since you still use Unity's JsonUtility and convert your date several times to / from json. Of course this should work, though it not that efficient ^^.


Anyways your Load method return the loaded "BuildingData" list. However you call your Load method inside Start but you essentially do nothing with the returned List. So I guess that's your issue?


You probably want to do something like this:

 void Start() {
     buildingData = Load<BuildingData>("key1");
 }

instead of

 void Start() {
     Load<BuildingData>("key1");
 }


edit

Since you still use Unity's JsonUtility, wouldn't it be simpler to just create a root class that contains the List you want to save?

 [System.Serializable]
 public class SaveDataRoot
 {
     public List<BuildingData> buildings = new List<BuildingData>();
 }

Unity's JsonUtility can serialize this class just fine. Just keep in mind that the top most node has to be an object and not an array. The only reason for your workaround would be that you really need an array as top most element for other reasons.


Another option would be to use the Json.NET library instead.

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

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

140 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 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

Why is JSON not saving my Class? 2 Answers

How i can PlayerPrefs List<>? 0 Answers

A node in a childnode? 1 Answer

Save and Load Function 2 Answers

How to save and load `List myList`? 1 Answer


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