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 Gilead7 · Jul 16, 2016 at 07:28 PM · c#savejson

Inserting JSON and saving new values

PROJECT STATUS:

I was able to create an initial json object, insert values into it and save it.

Now I want to add a new entry to it and save that, and so on, and so on. WriteAllText will overwrite the current file. I've tried AppendAll, which kinda works, but I'd had some odd results. One field would save old data even after the variable values were changed.

 public string GroupName="6th Batalion";
     public string GroupMemberID="TG";
     public string GroupMemberName="Tommy Gun";
     public string GroupEquipmentID="TG1";
     public string GroupEquipmentDescription="AR-9 machine gun";
 
 
 void Start () 
     {
         Testjson();
     }
     
     public void Testjson()
     {
         var groupdata = new JSONObject
         {
             {"GroupID", GroupID},
             {"GroupMemberName" , GroupMemberName},
 
         };
 
         var equipmentdata = new JSONObject
         {
                 {"EquipmentID" , GroupEquipmentID},
                  {"EquipmentDescription" , GroupEquipmentDescription},
                 
         };
 
         var maindata = new JSONObject
         {
             {"Group Name" , GroupName},
             {"Group Member" , groupdata},
             {"Group Equipment" , equipmentdata}
         };
 
 
 
         //return maindata;
         Debug.Log(maindata);
         File.WriteAllText(Application.dataPath + "/bandtest.json", maindata.ToString()); 
 
     }

Results: {"Group Name":"6th Batalion","Group Member":{"GroupMemberID":"TG","GroupMemberName":"Tommy Gun"},"Group Equipment":{"EquipmentID":"TG1","EquipmentDescription":"AR-9 machine gun"}} Works great here! Now we change the variables public string GroupName="6th Batalion"; public string GroupMemberID="STF"; public string GroupMemberName="SpikeHawk ThunderFist"; public string GroupEquipmentID="STF1"; public string GroupEquipmentDescription="Vulcan Cannon";

What's the best way to save the new data and keep the old data? Do I make two separate json files and create a third with all of them? Thanks!

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
2
Best Answer

Answer by QuiZr · Jul 17, 2016 at 03:07 PM

Parse old data, make changes, serialize, save.

Also, You shouldn't combine path like this (#48):

 File.WriteAllText(Application.dataPath + "/bandtest.json", maindata.ToString());

But instead use System.IO.Path.Combine(). Also, Application.persistentDataPath is the "right place" to save data.

 var path = System.IO.Path.Combine(Application.persistentDataPath, "bandtest.json");
 File.WriteAllText(path, maindata.ToString());
Comment
Add comment · Show 4 · 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 Gilead7 · Jul 17, 2016 at 06:11 PM 0
Share

So don't use File.Append?

avatar image QuiZr Gilead7 · Jul 17, 2016 at 06:27 PM 0
Share

Just overwrite it every time. Remember to put File.WriteAllText() in try-catch block for catching all exceptions that can occur (out of space, file already in use, file system stuff etc.).

avatar image Gilead7 · Jul 18, 2016 at 04:17 PM 0
Share

I've heard of try/catch blocks, but never used them.

avatar image YoYoYoYo_111_PiPi Gilead7 · Oct 28, 2016 at 11:28 PM 0
Share

I don't fully understand the answer from above, but just wanted to check if it worked? Did you manage not to lose your previous data and add new atop of it?

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

181 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Position Translation problem 1 Answer

Unity Saved Serialized Levels 0 Answers

How to save and load any data type? 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