Trying to save a list as json file and load it in C#(unity)

Viewed 104
public class Mycustom_gameobject_list
{
    public WayspotAnchorPayload game_object_coordinates;
    public GameObject storing_gameobject;
   
}
[Serializable]
public class Gameobject_lsit : MonoBehaviour
{
    public List<Mycustom_gameobject_list> Store_gameobjects_list = new List<Mycustom_gameobject_list>();
}

I create a list with a custom class and store some data inside then try to pass the list to the method that is created

 public static void SaveLocalPayloads(List<Mycustom_gameobject_list> gamelist)
        {
            List<Mycustom_gameobject_list> gameobjdata = new List<Mycustom_gameobject_list>();
            foreach (var Gamelist in gamelist)
            {
               gameobjdata.Add(Gamelist);
             
            }
         
            var json = JsonUtility.ToJson(gameobjdata);
           PlayerPrefs.SetString(GameobjData, json);
           
            //Debug.Log("Data Center length "+ gameobjdata.Count);
            Debug.Log("what's inside the file "+json);
            //var wayspotAnchorsData = new WayspotAnchorsData();
            //Debug.Log("DataCenter game obj" + gameobj.Length);
            //wayspotAnchorsData.Payloads = wayspotAnchorPayloads.Select(a => a.Serialize()).ToArray();
            //wayspotAnchorsData.gameobj = gameobj.ToArray();
            //Debug.Log("DataCenter Pay load length " + wayspotAnchorsData.Payloads.Length);
            //Debug.Log("DataCenter gameobj length " + wayspotAnchorsData.gameobj.Length);

            //string wayspotAnchorsJson = JsonUtility.ToJson(wayspotAnchorsData);
            //string obj = JsonUtility.ToJson(gameobj);
            //PlayerPrefs.SetString(DataKey, wayspotAnchorsJson);
            //PlayerPrefs.SetString(gamobj, obj);

        }

Then I debug the length of the list making sure the data is loading inside, and then I save it as json.

public static List<Mycustom_gameobject_list> LoadLocalPayloads()
        {
        if (PlayerPrefs.HasKey(GameobjData))
        {
            var json = PlayerPrefs.GetString(GameobjData);
            List<Mycustom_gameobject_list> gameobjectlist = JsonUtility.FromJson<List<Mycustom_gameobject_list>>(json);
            Debug.Log("Loading Data " + gameobjectlist);
            Debug.Log("The length of the loading data "+ gameobjectlist.Count);
            //var payloads = new List<WayspotAnchorPayload>();
            //var json = PlayerPrefs.GetString(DataKey);
            //var wayspotAnchorsData = JsonUtility.FromJson<WayspotAnchorsData>(json);
            //foreach (var wayspotAnchorPayload in wayspotAnchorsData.Payloads)
            //{
            //var payload = WayspotAnchorPayload.Deserialize(wayspotAnchorPayload);
            //payloads.Add(payload);
            //}
           // List<Mycustom_gameobject_list> load_gameobject_list = new List<Mycustom_gameobject_list>();
           
            /*foreach (var gameobject in gameobjectlist)
            {
                load_gameobject_list.Add(gameobject);
            }*/
            // var gameobjdata = JsonUtility.FromJson<Mycustom_gameobject_list>(json);
            //foreach (var gameobjects in gameobjdata.)
            
            

                return gameobjectlist;
        }
        else
        {
            return null;
        }
       However, when I debug the length of the data that is about to be loaded, it returns 0.

Not sure what is the issue and do I have to create a custom class in the script that stores the data? Thanks

1 Answers

If I get it correctly, you mean deserialize an C# object to json and serialized to JSON to C# object? In this case, I'd use Newtonsoft.Json .

Here is a generic list populated with 20 FooClass objects:

var barList = new List<FooClass>();
for (var i = 0; i < 20; i++)
{
    barList.Add(new FooClass { Index = i, Name = $"Name{i}" });
}

class FooClass
{
    public int Index { get; set; }

    public string Name { get; set; }
}

To serialize the list (make it to JSON string):

var jsonStr = JsonConvert.SerializeObject(barList);

You can write the value of jsonStr to a file, or wherever you can fetch it again to get the exact same value as jsonStr.

And you can just deserialize the JSON string to object (make string to object)

var jsonStrRead = File.ReadAllText("/foobar.json");
var deserializedBarList = JsonConvert.DeserializeObject<List<FooClass>>(jsonStrRead);

jsonStrRead is the content of saved file, deserializedBarList is the product of the deserialization.

Related