[英]Null problem when (de)serializing multiple objects with json
[SOLVED] applied given solution, works fine! [解决]应用了给定的解决方案,效果很好!
Aim of the program: Save/reload previous data when user opens and closes the program. 该程序的目的:当用户打开和关闭该程序时,保存/重新加载以前的数据。
I used to (de)serialize successfully with one object ( obj
), now I have two different objects of different classes. 我曾经使用一个对象 (
obj
) 成功地对(反)序列化,现在我有两个不同类的不同对象。
I tried to combine them by looking at other posts; 我试图通过查看其他帖子将它们组合在一起; I put them in object array and give that object array when (de)serializing as parameter.
我将它们放在对象数组中,并在将(反)序列化为参数时提供该对象数组。
I do initialize for example like this;
我确实像这样初始化。
obj.flag1 = true;
before callingserialize()
in other methods.(I didn't put them for simplicity since I already stated the functionality of methods)在其他方法中调用
serialize()
之前。(因为我已经说明了方法的功能,所以我没有为了简单起见就把它们放进去)
It says objects are null, but logically if obj2 and obj were null, it should have given the error for obj standalone. 它说对象为空,但是从逻辑上讲,如果obj2和obj为空,则应该为obj独立给出错误。 It won't read empty file I handled it.
它不会读取我处理过的空文件。 The moment I tried to combine two object it started to give me null error for both of them.
当我尝试组合两个对象的那一刻开始,这两个对象都给了我null错误。 I am about to rip my hair, can someone please help?
我将要扯头发,有人可以帮忙吗?
[Serializable]
public partial class UI : Form
{
FlagClass obj;
CurrentAmplitude obj2;
object[] seri_obj;//combine multiple objects
//deserialization is performed in constructor
public UI()
{
InitializeComponent();
seri_obj = new object[] { obj, obj2 };
input += ".txt";
//default path + new filename
path_combined = Path.Combine(root, input);
//it won't try to read empty file
if (!File.Exists(path_combined))
{
using (var stream = File.Create(path_combined))
{
}
}
else //already have that file,so when user opens the program, data is loaded from file
{
//read booleans and inetegres
string json2 = File.ReadAllText(path_combined);
string FormattedJson = FormatJson(json2);
seri_obj = JsonConvert.DeserializeObject<object[]>(FormattedJson);
}
}
private static string FormatJson(string json)
{
dynamic parsedJson = JsonConvert.DeserializeObject(json);
return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
}
//I do serialization here
void Serialize()
{
string json = JsonConvert.SerializeObject(seri_obj, Formatting.Indented);
File.WriteAllText(path_combined, json);
}
String values are in this class via "obj2"
字符串值通过“ obj2”在此类中
[Serializable]
class CurrentAmplitude
{
//this class has the string values
[JsonProperty(PropertyName = "value1")]
public int value1 { get; set; }
[JsonProperty(PropertyName = "value2")]
public string value2 { get; set; }
[JsonProperty(PropertyName = "value3")]
public string value3 { get; set; }
[JsonProperty(PropertyName = "value4")]
public string value4 { get; set; }
[JsonProperty(PropertyName = "value5")]
public string value5 { get; set; }
public CurrentAmplitude(){
}
}
Boolean values are in this class via "obj"
布尔值通过“ obj”在此类中
[Serializable]
class FlagClass
{
//this class has the boolean values
[JsonProperty(PropertyName = "flag1")]
public bool flag1 { get; set; }
[JsonProperty(PropertyName = "flag2")]
public bool flag2 { get; set; }
public FlagClass()
{
}
}
Where you are deserializing:- 在反序列化的地方:
seri_obj = JsonConvert.DeserializeObject<object[]>(FormattedJson);
You are asking the deserializer to return an array raw objects, which will result in an array of JObject
types, not your FlagClass
and CurrentAmplitude
types. 您
JObject
解串器返回一个数组原始对象,这将导致一个JObject
类型的数组,而不是FlagClass
和CurrentAmplitude
类型。
You're also setting seri_obj
, but never assigning the values in seri_obj
to your obj
or obj2
variables, which is why the compiler is warning you. 您还要设置
seri_obj
,但不要将seri_obj
的值分配给obj
或obj2
变量,这就是编译器警告您的原因。
You would be better off having an umbrella configuration class like this:- 您最好拥有这样的伞形配置类:-
class Configuration
{
public Flag { get; set; } = new FlagClass();
public CurrentAmplitude { get; set; } = new CurrentAmplitude();
}
Then just deserialize/serialize an instance of your Configuration
class when you want to load/save... 然后,当您要加载/保存时,只需反序列化/序列化
Configuration
类的实例即可。
// create config object if new
var config = new Configuration();
// to save
var json = JsonConvert.SerializeObject(config);
// to load
var config = JsonConvert.DeserializeObject<Configuration>(json);
// get/set config values
config.Flag.flag2 = false;
Here is a more complete example:- 这是一个更完整的示例:
void Main()
{
// create a new blank configuration
Configuration config = new Configuration();
// make changes to the configuration
config.CurrentAmplitude.value1 = 123;
config.CurrentAmplitude.value2 = "Hello";
config.FlagClass.flag1 = false;
config.FlagClass.flag2 = true;
// serialize configuration to a string in order to save to a file
string json = JsonConvert.SerializeObject(config);
// reload config from saved string
config = JsonConvert.DeserializeObject<Configuration>(json);
// should print "Hello"
Console.WriteLine(config.CurrentAmplitude.value2);
}
class Configuration
{
public CurrentAmplitude CurrentAmplitude { get; set; } = new CurrentAmplitude();
public FlagClass FlagClass { get; set; } = new FlagClass();
}
class CurrentAmplitude
{
public int value1 { get; set; }
public string value2 { get; set; }
public string value3 { get; set; }
public string value4 { get; set; }
public string value5 { get; set; }
}
class FlagClass
{
public bool flag1 { get; set; }
public bool flag2 { get; set; }
}
Pre C# 6, your config class would look like this:- 在C#6之前的版本中,您的配置类如下所示:
class Configuration
{
public Configuration()
{
CurrentAmplitude = new CurrentAmplitude();
FlagClass = new FlagClass();
}
public CurrentAmplitude CurrentAmplitude { get; set; }
public FlagClass FlagClass { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.