Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用json(反)序列化多个对象时出现空问题_C#_Json_Serialization_Deserialization - Fatal编程技术网

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

C# 使用json(反)序列化多个对象时出现空问题,c#,json,serialization,deserialization,C#,Json,Serialization,Deserialization,[已解决]应用给定的解决方案,效果很好 程序的目的:当用户打开和关闭程序时,保存/重新加载以前的数据 我曾经用一个对象(obj)成功地(反)序列化,现在我有两个不同类的不同对象 我试图通过查看其他帖子将它们结合起来;我将它们放在对象数组中,并在(反)序列化为参数时给出该对象数组 例如,我会这样初始化obj.flag1=真之前 在其他方法中调用serialize()。(为了简单起见,我没有将它们放在这里,因为我已经说明了方法的功能) 它说对象为null,但从逻辑上讲,如果obj2和obj为null

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

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

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

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

例如,我会这样初始化<代码>obj.flag1=真之前 在其他方法中调用
serialize()
。(为了简单起见,我没有将它们放在这里,因为我已经说明了方法的功能)

它说对象为null,但从逻辑上讲,如果obj2和obj为null,它应该给出obj standalone的错误。它不会读取我处理过的空文件。当我尝试组合两个对象时,它开始给我两个对象的空错误。我要扯头发了,有人能帮我吗

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

        }
布尔值通过“obj”在此类中


反序列化的位置:-

seri_obj = JsonConvert.DeserializeObject<object[]>(FormattedJson);
然后,当您要加载/保存时,只需反序列化/序列化
配置
类的实例

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

为什么要反序列化,然后序列化,再反序列化同一个json?@PommeDeTerre因为当用户打开和关闭此程序时,数据应该保存在停止的位置,这意味着变量是以先前初始化的方式获取的,但当文件存在时,您会将其读入json2,然后将其传递到FormatJson,在那里对其进行反序列化,然后再次序列化,最后将其反序列化为seri_obj。为什么不直接编写seri_obj=JsonConvert.DeserializeObject(json2)@FrancescoBaruchelli这只是为了格式化显示json对象,但即使我不使用它,只是应用你的,对象看起来仍然为空。我知道FormatJson方法的目的,它当然不是问题的原因,但以这种方式使用它是无用的,消耗资源,并且使您的代码更难理解。谢谢,但是为了使其他类对
UI
可见,我使用了
FlagClass obj
currentAmplication obj2
,要应用您的解决方案,我必须删除它们吗?43个方法正在使用obj,而且我不能将
配置
类放在同一名称空间中,它说您不能像那样使用
新建
。为什么添加配置不可行?无论如何,您不必删除obj和obj1(顺便说一句,这些都是非常糟糕的名称),只需添加obj=config.Flag;obj2=配置电流振幅;经过反序列化配置后。@FrancescoBaruchelli顺便说一句,此配置类语法给出错误,您是说public
currentAmpliance obj{get;set;}=new currentAmpliance()使用我回答中的示例,您应该只需要引用
配置
类。。。您始终可以从那里访问配置类中的属性和(子)类。您可以去掉
obj
obj2
,只需引用您创建的
config
变量(用于新的intall)或反序列化(从保存的文件加载时)@Selen哦,我现在看到问题了,您有
public currentAmplification{get;set;}
而不是
公共电流幅度电流幅度{get;set;}
。。。它缺少属性名
seri_obj = JsonConvert.DeserializeObject<object[]>(FormattedJson);
class Configuration
{
    public Flag { get; set; } = new FlagClass();

    public CurrentAmplitude { get; set; } = new CurrentAmplitude();
}
// 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; }
}
class Configuration
{
    public Configuration()
    {
        CurrentAmplitude = new CurrentAmplitude();
        FlagClass = new FlagClass();
    }

    public CurrentAmplitude CurrentAmplitude { get; set; }

    public FlagClass FlagClass { get; set; }
}