Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Getting an error while loading JSON from resources folder in WebGL build

Discussion in 'Web' started by roopesh23, Dec 2, 2023.

  1. roopesh23

    roopesh23

    Joined:
    Jul 11, 2022
    Posts:
    10
    While running on the editor, the strings stored in JSON is able to load, but when I am running on browser it's giving an error. I have tried to look for some solution but nothing good. Sharing a screenshot of the error and the script where I am loading the JSON.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public static class JsonLoader
    4. {
    5.     public static string LoadJson(string loadPath)
    6.     {
    7.         TextAsset data = Resources.Load<TextAsset>(loadPath);
    8.         return data.text;
    9.     }
    10. }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,579
    What's the actual path?
     
  3. roopesh23

    roopesh23

    Joined:
    Jul 11, 2022
    Posts:
    10
    The load path? It's the name of the JSON file stored in resources folder
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,579
    So loadPath is something like this: "myfile.json"?

    Hmmm I don't think the path is the issue. I looked at the error again and noticed it fails to create a List, and that you are using Newtonsoft Json. Are you using the official Newtonsoft package? If not, I would try that. It may be more compatible with WebGL.

    Unless you have specific requirements you can also use JsonUtility, it has some limits but if all you need to do is load a list of strings for instance, it'll be fine for that as long as the list is part of a serialized class. JsonUtility cannot serialize collections directly.
     
  5. roopesh23

    roopesh23

    Joined:
    Jul 11, 2022
    Posts:
    10
    So I have an array of strings in my JSON which I am loading into a Queue. I am using official Newtonsoft package. No the list is not part of a serialized class. Let me show how I am loading the data into the queue, maybe it can give more context

    A snippet of code, but you can see I am using "DetectiveBegin.json" to load into the string, which is deserialized into the queue!

    Code (CSharp):
    1. using UnityEngine;
    2. using Newtonsoft.Json;
    3. using System.Collections.Generic;
    4.  
    5. namespace MysteryMayhem.Dialogue
    6. {
    7.     public class DialogueLoader : MonoBehaviour
    8.     {
    9.         #region ---------- Public Methods ----------
    10.         public Queue<string> GetDetBeginQueue()
    11.         {
    12.             string detString = JsonLoader.LoadJson("DetectiveBegin");
    13.             Queue<string> detectiveQueue = JsonConvert.DeserializeObject<Queue<string>>(detString);
    14.             return detectiveQueue;
    15.         }
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,579
    I see. It would be helpful to know whether the exception occurs during LoadJson or DeserializeObject.

    If you haven't already: enable full stacktrace Exception handling in the Player Settings, this is near the bottom of all the settings.

    But here you could just Debug.Log(detString) and see if this appears or not.
    You could also try, for a test, to deserialize it into List<string> instead. I'm assuming it wouldn't matter because a Queue is just a List in Json format. If this works, you could then initialize a Queue with the contents of the List.
     
    roopesh23 likes this.
  7. roopesh23

    roopesh23

    Joined:
    Jul 11, 2022
    Posts:
    10
    Thanks so much @CodeSmile for the advise, it worked. Yes the problem was during Deserialization of object. I stored it in List and then put in Queue to load, it worked fine.