can someone tell me how to deserialize a json object using either a path to another json file or a json object?
I am trying to make a game where its entirely constructed of config files.
basically I would take either these two...
{
"LevelConfig" : {
"SkillGain" : 0.01,
"MagicGain" : 0.01,
"XpScale" : 50.0,
"HpScale" : 0.01,
"MpScale" : 0.01
}
}
or
{
"LevelConfig" : "../Level.level" (only returns json object if file exists else it tries to return string)
}
and convert them into these
public class JLevel
{
public float SkillGain { get; set; }
public float MagicGain { get; set; }
public float XpScale { get; set; }
public float HpScale { get; set; }
public float MpScale { get; set; }
}
public class JEntity
{
public string Name { get; set; }
public int Hp { get; set; }
public int Mp { get; set; }
public int Xp { get; set; }
public JLevel LevelConfig { get; set; }
public JColor ColorConfig { get; set; }
public JPhysics PhysicsConfig { get; set; }
}
class Entity
{
public Entity Load(string filename)
{
return ???;
}
public void Save(string filename)
{
???;
}
}
now I know I can just use JsonConvert.DeserializeObject()
but that doesn't check for json paths.
First you need to create a class for this Json Object
The name of class should be the same as Object name in JSON
{
"LevelConfig" : {
"SkillGain" : 0.01,
"MagicGain" : 0.01,
"XpScale" : 50.0,
"HpScale" : 0.01,
"MpScale" : 0.01
}
}
public class LevelConfig
{
public double SkillGain { get; set; }
public double MagicGain { get; set; }
public double XpScale { get; set; }
public double HpScale { get; set; }
public double MpScale { get; set; }
}
public class Example
{
public LevelConfig LevelConfig { get; set; }
}
then you need to desalinize it using Newtonsoft
JsonConvert.DeserializeObject<Example>(Json);
Json should be a string. If its an Object the you need to use this on
JsonConvert.DeserializeObject<Example>(JsonConvert.SerializeObject(Json));
class Program
{
public static void Main()
{
var str = "{ LevelConfig : \"level.json\"}";
var levelConfigs = JsonConvert.DeserializeObject<LevelConfigs>(str);
var fileTxt = GetFileText(levelConfigs.LevelConfig);
JLevel level = JsonConvert.DeserializeObject<JLevel>(fileTxt);
Console.WriteLine(level.HpScale);
Console.ReadLine();
}
private static string GetFileText(string jsonFile)
{
return File.ReadAllText(jsonFile);
}
}
public class LevelConfigs
{
public string LevelConfig { get; set; }
}
public class JLevel
{
public float SkillGain { get; set; }
public float MagicGain { get; set; }
public float XpScale { get; set; }
public float HpScale { get; set; }
public float MpScale { get; set; }
}
public class JEntity
{
public string Name { get; set; }
public int Hp { get; set; }
public int Mp { get; set; }
public int Xp { get; set; }
public JLevel LevelConfig { get; set; }
}
you can change your implementation of GetFileText for your needs
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.