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
2
Question by matiasmarc · Oct 30, 2017 at 08:27 AM · androidunity 5json

How to save and load a json file on android

Hello I'm making an android game but the game wont load save files which is a json file some people say that i use resource.load but thats only for read only files and Ii'm trying to load a save file.

Can someone please help with the code for proper json file saving and loading I've also used File.Readalltext and File.Writealltext but cant read the json file

Someone please help

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
4

Answer by WinterboltGames · Oct 30, 2017 at 08:36 AM

here's a code snippet:

 using UnityEngine;
 using System.IO;
 
 public class JsonFileWriter : MonoBehaviour
 {
     public JsonData data;
 
     public string path;
 
     public void Start ()
     {
         data = new JsonData(1, "Alfred Jodl");
 
         path = Path.Combine(Application.persistantDataPath, "saved files", "data.json");

         SerializeData();

         DeserializeData();
     }
 
     public void SerializeData ()
     {
         string jsonDataString = JsonUtility.ToJson(data, true);
         
         File.WriteAllText(path, jsonDataString);
 
         Debug.Log(jsonDataString);
     }
 
     public void DeserializeData ()
     {
         string loadedJsonDataString = File.ReadAllText(path);
 
         data = JsonUtility.FromJson<JsonData>(loadedJsonDataString);
 
         Debug.Log("id: " + data.id.ToString() + " | name: " + data.name)
     }
 }
 
 [Serializeable]
 public class JsonData 
 {
     public int id;
     public string name;
 
     public JsonData (int id, string name)
     {
         this.id = id;
         this.name = name;
     }
     
     public JsonData (){}
 }
Comment
Add comment · Show 7 · 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 WinterboltGames · Oct 30, 2017 at 08:37 AM 0
Share

should work flawlessly!

avatar image matiasmarc WinterboltGames · Oct 31, 2017 at 07:13 AM 0
Share

No overload method 'Combine' takes 3 arguments

avatar image matiasmarc WinterboltGames · Oct 31, 2017 at 07:16 AM 0
Share

Application.persistantDataPath how to do you create a save file with 'N - coins' at the start?

avatar image x4637x matiasmarc · Oct 31, 2017 at 09:05 AM 0
Share

That Path.Combine(Application.persistantDataPath, "saved files", "data.json"); is just the same as Application.persistantDataPath + "/saved files" + "/data.json" on PC. But it could be useful if your system are using "\" ins$$anonymous$$d of "/".

That data.json is (save filename).(format extension), so if you want to create something like "N - coins.json" you could simply just replace the "data" to your 'N - coins'.

Show more comments
avatar image andycodes · Apr 22, 2018 at 04:35 PM 0
Share

This is super helpful!

avatar image Trost_ · May 27, 2020 at 11:47 AM 0
Share

You just made my day, thanks!

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

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

Windows Phone 8.1 WWW post request error 0 Answers

Unity can read Json file, but Android not. 2 Answers

LitJSON android problem 1 Answer

How to do communication between apps. ( Android ) 2 Answers

Unity project on android - OnPointerDown/Up movement 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