繁体   English   中英

使用json(反序列化)多个对象时出现空问题

[英]Null problem when (de)serializing multiple objects with json

[解决]应用了给定的解决方案,效果很好!

该程序的目的:当用户打开和关闭该程序时,保存/重新加载以前的数据。

我曾经使用一个对象obj成功地对(反)序列化,现在我有两个不同类的不同对象。

我试图通过查看其他帖子将它们组合在一起; 我将它们放在对象数组中,并在将(反)序列化为参数时提供该对象数组。

我确实像这样初始化。 obj.flag1 = true; 在其他方法中调用serialize()之前。(因为我已经说明了方法的功能,所以我没有为了简单起见就把它们放进去)

它说对象为空,但是从逻辑上讲,如果obj2和obj为空,则应该为obj独立给出错误。 它不会读取我处理过的空文件。 当我尝试组合两个对象的那一刻开始,这两个对象都给了我null错误。 我将要扯头发,有人可以帮忙吗?

 [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);

        }

字符串值通过“ 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(){

        }
    }

布尔值通过“ 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()
        { 

        }

    }

空对象警告

在反序列化的地方:

seri_obj = JsonConvert.DeserializeObject<object[]>(FormattedJson);

JObject解串器返回一个数组原始对象,这将导致一个JObject类型的数组,而不是FlagClassCurrentAmplitude类型。

您还要设置seri_obj ,但不要将seri_obj的值分配给objobj2变量,这就是编译器警告您的原因。

您最好拥有这样的伞形配置类:-

class Configuration
{
    public Flag { get; set; } = new FlagClass();

    public CurrentAmplitude { get; set; } = new CurrentAmplitude();
}

然后,当您要加载/保存时,只需反序列化/序列化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;

这是一个更完整的示例:

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; }
}

在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.

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