简体   繁体   中英

c# With Unity and LitJSON Getting an Invalid Cast Exception when trying to de-serialize back from JSONData object

I have been working on a save game solution for my project and have hit a wall. After several days of research, trial/ error etc. I decided to give the stack a shot. I am attempting to convert back from a Json text file into an object, then add it to a dictionary. It keeps giving my Invalid cast exceptions, regardless as to how I iterate through the Jdata object. Keeping in mind I am using LitJson 3rd party. Here is the code thus far. The error occurs at the foreach statement.

 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using LitJson;
    using System.IO;

    public static class SaveGame  {


    public static string savePath = Application.persistentDataPath + 
   "/Saves/";
    public static int numberOfSaves = 0;
    public static string saveFileName = PlayerCrew.playerShipName + ".json";

    public static void SavePlayerData ()
    {
        string playerSavePath = savePath + saveFileName;
        string jsonHolder;

        jsonHolder = JsonMapper.ToJson(PlayerCrew.playerCrewManifest);

        if (!File.Exists(playerSavePath))
        {
            FileStream fs = new FileStream(playerSavePath, 
    FileMode.OpenOrCreate);
            fs.Close();
            File.WriteAllText(playerSavePath, jsonHolder);          

        }
        else
        {
            File.WriteAllText(playerSavePath, jsonHolder);
        }

     }

     public static void LoadCrewManifest()
     {
        string playerSavePath = savePath + saveFileName;
        string jsonHolder;

        jsonHolder = File.ReadAllText(playerSavePath);
        JsonData jdata = JsonMapper.ToObject(jsonHolder);
        PlayerCrew.playerCrewManifest.Clear();

        foreach (KeyValuePair<string,CrewMember> item in jdata)
        {
            PlayerCrew.playerCrewManifest.Add(item.Key, item.Value);
            Debug.Log(item.Key);
        }

    }





    }

I recommend you to use NetJson . You can Deserialize using a generic type, including Dictionary.

The values in jdata might come as KeyValuePair<string, string>

the simplest way would be a simple constructor for your class CrewMember eg something like

[Serializable]
public class CrewMember
{
    public string Name;

    public CrewMember(string name)
    {
        Name = name;
    }
}

Than you don't want the item.key since it will be the variable name (in this case Name ) instead you want the item.Value .

Your json code could look like

JsonData jdata = JsonMapper.ToObject(jsonHolder);
PlayerCrew.playerCrewManifest.Clear();

foreach (KeyValuePair<string,string> item in jdata)
{
    PlayerCrew.playerCrewManifest.Add(item.Value, new CrewMember(item.Value));
    Debug.Log(item.Value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM