[英]json.net, how do I deserialize json object from relative path or actual json object
can someone tell me how to deserialize a json object using either a path to another json file or a json object?有人可以告诉我如何使用另一个 json 文件的路径或 json 对象来反序列化 json 对象吗?
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.现在我知道我可以只使用
JsonConvert.DeserializeObject()
但这不会检查 json 路径。
First you need to create a class for this Json Object首先你需要为这个 Json 对象创建一个类
The name of class should be the same as Object name in JSON
类的名称应与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那么你需要使用Newtonsoft对其进行脱盐处理
JsonConvert.DeserializeObject<Example>(Json);
Json should be a string.
Json 应该是一个字符串。 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您可以根据需要更改 GetFileText 的实现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.