Search Unity

ArgumentException: JSON parse error: The document root must not follow by other values.

Discussion in 'Scripting' started by 6a6179, Aug 17, 2019.

  1. 6a6179

    6a6179

    Joined:
    Jul 6, 2014
    Posts:
    4
    Hi everyone.

    Just to firstly clarify I have researched extensively for solutions to this problem and have tried several different solutions however had either received the same error(s) or unexpected functionality from these.

    What I'm simply trying to do is to add a highscore after the game has finished to JSON, Binary, etc. file. When the game is played several times, i want to be able to retrieve from JSON, Binary etc. file for all of the highscores and display them in a 'highscores' table.

    I so far can save a highscore to JSON (preferred for ease of checking the contents and structure of the JSON file) however when reading the data back using Debug.Log, i can only retrieve the first element. If there is more than 1 element in the JSON, i receive the ArgumentException error message shown in the title of this posting.

    This is how the JSON file looks after the latest code change:

    {
    "players": [
    {
    "name": "You",
    "rank": 1,
    "currentScore": 200,
    "currentXp": 200
    }
    ]
    }{
    "players": [
    {
    "name": "You",
    "rank": 2,
    "currentScore": 300,
    "currentXp": 500
    }
    ]
    }

    The only variation with this with different versions of code is:
    {
    "name": "You",
    "rank": 1,
    "currentScore": 200,
    "currentXp": 200
    }{
    "name": "You",
    "rank": 1,
    "currentScore": 200,
    "currentXp": 200
    }

    I have tried using List<thisObject> and thisObject when writing/reading to/from JSON file. I only want to add one highscore at a time however want to retrieve the contents as a list.

    I'm using JSONUtility however have also used the BinaryFormatter class when writing to Binary file.

    Thanks for reading
     
  2. 6a6179

    6a6179

    Joined:
    Jul 6, 2014
    Posts:
    4
    *UPDATE*

    I've resolved this problem myself. Thanks for anyone that has read or is reading this anyway.
     
  3. jasonmcev

    jasonmcev

    Joined:
    Mar 12, 2015
    Posts:
    3
    You resolved but decided to keep the solution to yourself?
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    If you're having the same issue, run your JSON through a JSON validator. It should point you to where your issue is occurring.

    I'm guessing that OP ran into issues where he was appending new scores directly to the file. This will result in invalid JSON, as a valid JSON file can only have one root object, and his file had two.

    Code (JSON):
    1. {
    2.  
    3. }
    vs.
    Code (JSON):
    1. {
    2.  
    3. }{
    4.  
    5. }
     
    Rakyta711 and the_real_ijed like this.
  5. PelusoWarro

    PelusoWarro

    Joined:
    May 12, 2016
    Posts:
    8
    Hello, i am experiencing the same error.
    I dont understand, i have plenty of similar requests and other clases running nicely.
    just a 3 atribute class. the JSON has been parsed and seems to be okay
    Code (CSharp):
    1. [System.Serializable]
    2. public class Scam
    3. {
    4.     public string emp;
    5.     public string datt;
    6.     public string fecha;
    7. }
    8.  
    9. [System.Serializable]
    10. public class ScamList
    11. {
    12.     public List<Scam> data;
    13.  
    14. }
    15.  
    16. //usage
    17.  ScamList icl = JsonUtility.FromJson<ScamList>(res);
    18.  
    Code (CSharp):
    1. {"data":[{"emp":"0","datt":"aasd","fecha":"19/01/2022 01:49"},{"emp":"0","datt":"ssss","fecha":"19/01/2022 01:49"}]}
    2.  
    the error:
    System.ArgumentException: JSON parse error: The document root must not follow by other values.
    at (wrapper managed-to-native) UnityEngine.JsonUtility.FromJsonInternal(string,object,System.Type)
    at UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) [0x00062] in C:\buildslave\unity\build\Modules\JSONSerialize\Public\JsonUtility.bindings.cs:42
    at UnityEngine.JsonUtility.FromJson[T] (System.String json) [0x0000c] in C:\buildslave\unity\build\Modules\JSONSerialize\Public\JsonUtility.bindings.cs:30
    at GameManager+<Upload>d__130.MoveNext () [0x00604] in C:\Users\Dani-SSM\Documents\UNITY\Validor\Assets\GameManager.cs:1032
    UnityEngine.MonoBehaviour:print(Object)
    <Upload>d__130:MoveNext() (at Assets/GameManager.cs:1077)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

    its kinda blowing my mind, i would appreciate your help :)
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Please don't necro-post three-year-old posts for completely new and separate problems.

    In this case you eitger have damaged or malformed JSON data, or it is not fully supported by the Unity "tiny lite" serializer.

    Problems with Unity "tiny lite" built-in JSON:

    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window -> Package Manager).

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    Also, always be sure to leverage sites like:

    https://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io