c# With Unity and LitJSON Getting an Invalid Cast Exception when trying to de-serialize back from JSONData object
up vote
0
down vote
favorite
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);
c# unity3d litjson
add a comment |
up vote
0
down vote
favorite
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);
c# unity3d litjson
1
what are your exact errors ? which line ? can you provide a JSON as well as the correpsonding class code ?
– LoneWanderer
Nov 12 at 0:44
Please show us the structure of theCrewMember
class
– derHugo
Nov 13 at 6:28
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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);
c# unity3d litjson
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);
c# unity3d litjson
c# unity3d litjson
asked Nov 12 at 0:07
Dave cook
11
11
1
what are your exact errors ? which line ? can you provide a JSON as well as the correpsonding class code ?
– LoneWanderer
Nov 12 at 0:44
Please show us the structure of theCrewMember
class
– derHugo
Nov 13 at 6:28
add a comment |
1
what are your exact errors ? which line ? can you provide a JSON as well as the correpsonding class code ?
– LoneWanderer
Nov 12 at 0:44
Please show us the structure of theCrewMember
class
– derHugo
Nov 13 at 6:28
1
1
what are your exact errors ? which line ? can you provide a JSON as well as the correpsonding class code ?
– LoneWanderer
Nov 12 at 0:44
what are your exact errors ? which line ? can you provide a JSON as well as the correpsonding class code ?
– LoneWanderer
Nov 12 at 0:44
Please show us the structure of the
CrewMember
class– derHugo
Nov 13 at 6:28
Please show us the structure of the
CrewMember
class– derHugo
Nov 13 at 6:28
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I recommend you to use NetJson. You can Deserialize using a generic type, including Dictionary.
Also, you can use PlayerPrefs.Set(key, myJson) since its a string. Then you can just use PlayerPrefs.Get to retrieve it
– Martin Gonzalez
Nov 12 at 1:39
Retrieving the Json string is not the issue. The issue is that it will not allow me to convert the text back to an object after I have loaded back from the file.Saving works just fine.
– Dave cook
Nov 12 at 11:40
Oh! so its how @derHugo said.
– Martin Gonzalez
Nov 13 at 13:11
add a comment |
up vote
0
down vote
The values in jdata
might come as KeyValuePair<string, string>
the simplest way would be a simple constructor for your class CrewMember
e.g. 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);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254496%2fc-sharp-with-unity-and-litjson-getting-an-invalid-cast-exception-when-trying-to%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I recommend you to use NetJson. You can Deserialize using a generic type, including Dictionary.
Also, you can use PlayerPrefs.Set(key, myJson) since its a string. Then you can just use PlayerPrefs.Get to retrieve it
– Martin Gonzalez
Nov 12 at 1:39
Retrieving the Json string is not the issue. The issue is that it will not allow me to convert the text back to an object after I have loaded back from the file.Saving works just fine.
– Dave cook
Nov 12 at 11:40
Oh! so its how @derHugo said.
– Martin Gonzalez
Nov 13 at 13:11
add a comment |
up vote
0
down vote
I recommend you to use NetJson. You can Deserialize using a generic type, including Dictionary.
Also, you can use PlayerPrefs.Set(key, myJson) since its a string. Then you can just use PlayerPrefs.Get to retrieve it
– Martin Gonzalez
Nov 12 at 1:39
Retrieving the Json string is not the issue. The issue is that it will not allow me to convert the text back to an object after I have loaded back from the file.Saving works just fine.
– Dave cook
Nov 12 at 11:40
Oh! so its how @derHugo said.
– Martin Gonzalez
Nov 13 at 13:11
add a comment |
up vote
0
down vote
up vote
0
down vote
I recommend you to use NetJson. You can Deserialize using a generic type, including Dictionary.
I recommend you to use NetJson. You can Deserialize using a generic type, including Dictionary.
answered Nov 12 at 1:36
Martin Gonzalez
945
945
Also, you can use PlayerPrefs.Set(key, myJson) since its a string. Then you can just use PlayerPrefs.Get to retrieve it
– Martin Gonzalez
Nov 12 at 1:39
Retrieving the Json string is not the issue. The issue is that it will not allow me to convert the text back to an object after I have loaded back from the file.Saving works just fine.
– Dave cook
Nov 12 at 11:40
Oh! so its how @derHugo said.
– Martin Gonzalez
Nov 13 at 13:11
add a comment |
Also, you can use PlayerPrefs.Set(key, myJson) since its a string. Then you can just use PlayerPrefs.Get to retrieve it
– Martin Gonzalez
Nov 12 at 1:39
Retrieving the Json string is not the issue. The issue is that it will not allow me to convert the text back to an object after I have loaded back from the file.Saving works just fine.
– Dave cook
Nov 12 at 11:40
Oh! so its how @derHugo said.
– Martin Gonzalez
Nov 13 at 13:11
Also, you can use PlayerPrefs.Set(key, myJson) since its a string. Then you can just use PlayerPrefs.Get to retrieve it
– Martin Gonzalez
Nov 12 at 1:39
Also, you can use PlayerPrefs.Set(key, myJson) since its a string. Then you can just use PlayerPrefs.Get to retrieve it
– Martin Gonzalez
Nov 12 at 1:39
Retrieving the Json string is not the issue. The issue is that it will not allow me to convert the text back to an object after I have loaded back from the file.Saving works just fine.
– Dave cook
Nov 12 at 11:40
Retrieving the Json string is not the issue. The issue is that it will not allow me to convert the text back to an object after I have loaded back from the file.Saving works just fine.
– Dave cook
Nov 12 at 11:40
Oh! so its how @derHugo said.
– Martin Gonzalez
Nov 13 at 13:11
Oh! so its how @derHugo said.
– Martin Gonzalez
Nov 13 at 13:11
add a comment |
up vote
0
down vote
The values in jdata
might come as KeyValuePair<string, string>
the simplest way would be a simple constructor for your class CrewMember
e.g. 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);
add a comment |
up vote
0
down vote
The values in jdata
might come as KeyValuePair<string, string>
the simplest way would be a simple constructor for your class CrewMember
e.g. 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);
add a comment |
up vote
0
down vote
up vote
0
down vote
The values in jdata
might come as KeyValuePair<string, string>
the simplest way would be a simple constructor for your class CrewMember
e.g. 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 values in jdata
might come as KeyValuePair<string, string>
the simplest way would be a simple constructor for your class CrewMember
e.g. 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);
edited Nov 13 at 6:31
answered Nov 13 at 6:24
derHugo
3,81421027
3,81421027
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254496%2fc-sharp-with-unity-and-litjson-getting-an-invalid-cast-exception-when-trying-to%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
what are your exact errors ? which line ? can you provide a JSON as well as the correpsonding class code ?
– LoneWanderer
Nov 12 at 0:44
Please show us the structure of the
CrewMember
class– derHugo
Nov 13 at 6:28