Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by AcidMage · Jun 22, 2021 at 07:00 AM · serializationjson

JSON error: Invalid value when loading file

Heya!

I'm trying to put together a save/load character system for my game, and I keep running into this error. I've made a fresh project to just test out the saving/loading data to try and focus in on the problem.

What I'm trying to achieve: players can set a few values (strings and ints) for their character, then save it out and load it in later. They do so by pressing a "save" button and a "load" button respectively, which call functions on the "CharInGame" script. For testing purposes, you just enter the character stats into the inspector.

What is happening: saving seems to be ok, but loading results in the error:

 ArgumentException: JSON parse error: Invalid value.
 UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <7a84dc901f21458f98a12e112d4ba2ab>:0)
 UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <7a84dc901f21458f98a12e112d4ba2ab>:0)
 CharInGame.LoadCharacter () (at Assets/CharInGame.cs:32)
 UnityEngine.Events.InvokableCall.Invoke () (at <fe84f4a754da4a6bb64fca409d40938a>:0)
 UnityEngine.Events.UnityEvent.Invoke () (at <fe84f4a754da4a6bb64fca409d40938a>:0)
 UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
 UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
 UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
 UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
 UnityEngine.EventS

Here are my scripts:

My Character Data class, "CharData": using System.Collections; using System.Collections.Generic; using UnityEngine;

 [System.Serializable]
 public class CharData
 {
     public string VarName;
     public int Power;
     public int Speed;
     public string Catchphrase;
 }

The script that handles saving and loading files: using System.Collections; using System.IO; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

 public class CharInGame : MonoBehaviour
 {
     public CharData Character;
     public string saveFile;
 
     public void SaveCharacter()
     {
         string jsonString = JsonUtility.ToJson(Character);
         Debug.Log(jsonString);
         saveFile = Application.persistentDataPath + "/CharData.json";
         File.WriteAllText(saveFile,jsonString);
     }
     public void LoadCharacter()
     {
         CharData LoadedCharacter = JsonUtility.FromJson<CharData>(saveFile);
     }
 }
 

The output I am getting from "Debug.Log(jsonString);" is (depending on my input) {"VarName":"Bill","Power":5,"Speed":10,"Catchphrase":"Thats a lotta spiders!"}"

Hope that is enough info!

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 AcidMage · Jun 22, 2021 at 09:24 PM

Found the problem - I was parsing the string of the directory itself as the json data! Changed my load function to:

 public void LoadCharacter()
     {
         string jsonToRead = File.ReadAllText(Application.persistentDataPath + "/CharData.json");
         CharData LoadedCharacter = JsonUtility.FromJson<CharData>(jsonToRead);
     }

Cheers!

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

168 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

Related Questions

Null Exception while serialize json string into txt file 0 Answers

Serialize gameobject children behaviours 0 Answers

[SOLVED] JSON Serialization of Derived Classes 2 Answers

UnityException: Failed to run serialization 0 Answers

Nested dictionary serialization 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