Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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# 将windows窗体文件内容保存到JSON文件中_C#_Json_Winforms - Fatal编程技术网

C# 将windows窗体文件内容保存到JSON文件中

C# 将windows窗体文件内容保存到JSON文件中,c#,json,winforms,C#,Json,Winforms,我有一个带有一些字段的windows窗体: 两个列表视图(输入字段和输出字段) 三个TexBox(标题1,标题2和标题3) 两个按钮(保存到文件和读取文件) 我的问题是: 当我点击保存到文件按钮时,我想将两个列表视图和三个文本框的所有内容保存到一个JSON文件中 当我单击从文件读取的时,我想从JSON文件填充列表视图和文本框 知道: { "title1": "example Title 1", "title2": "example Title 2", "title3


我有一个带有一些字段的windows窗体:

  • 两个列表视图
    输入字段
    输出字段

  • 三个TexBox
    标题1
    标题2
    标题3

  • 两个按钮
    保存到文件
    读取文件


我的问题是: 当我点击
保存到文件
按钮时,我想将两个
列表视图
和三个
文本框
的所有内容保存到一个JSON文件中

当我单击从文件读取的
时,我想从JSON文件填充
列表视图和
文本框

知道:

{
    "title1": "example Title 1",
    "title2": "example Title 2",
    "title3": "example Title 3",

    "inputfields": {
        "item1": {
                    "name": "SGML",
                    "tag": "SGML"
                },
        "item2": {
                    "name": "SGML",
                    "tag": "SGML"
                },          
        ......

        "itemN": {
                    "name": "SGML",
                    "tag": "SGML"
                 }
           },

    "outputfields": {
        "item1": {
                    "name": "SGML",
                    "tag": "SGML"
                },
        "item2": {
                    "name": "SGML",
                    "tag": "SGML"
                },          
        ......

        "itemM": {
                    "name": "SGML",
                    "tag": "SGML"
                }
    }
}
我将
Id
保存在
列表视图的每个项目的
标签上


我的JSON文件的结构:

{
    "title1": "example Title 1",
    "title2": "example Title 2",
    "title3": "example Title 3",

    "inputfields": {
        "item1": {
                    "name": "SGML",
                    "tag": "SGML"
                },
        "item2": {
                    "name": "SGML",
                    "tag": "SGML"
                },          
        ......

        "itemN": {
                    "name": "SGML",
                    "tag": "SGML"
                 }
           },

    "outputfields": {
        "item1": {
                    "name": "SGML",
                    "tag": "SGML"
                },
        "item2": {
                    "name": "SGML",
                    "tag": "SGML"
                },          
        ......

        "itemM": {
                    "name": "SGML",
                    "tag": "SGML"
                }
    }
}
有什么功能可以为我做这项工作


感谢Stackoverflowers。

您可以使用一个在后台保存数据的类,并使用类似JSON.NET的库将该类序列化为JSON并返回


您可以使用一个在后台保存数据的类,并使用类似JSON.NET的库将该类序列化为JSON并返回

好的,很简单

您需要做的是首先创建一个名为field的对象,其中包含Item对象。然后Item对象有两个属性:tag和name。像这样的。但请注意,我没有测试代码,我只是在记事本上写的。但应该是这样的

导入此库:使用System.Web.Script.Serialization

然后创建这个类:

    public class Field
{
    public Field(){}
    public Item item1{set; get;}
    public Item item2{set; get;}
    public Item item3{set; get;}

}
还有一个叫做item的类

public class Item
    {
        public Item(){}
        public string name{set; get;}
        public string Tag{set; get;}

    }
然后像这样:

List<Field> Items = new List<Field>{
                   new Field{item1 = new Item{name:title1.Text, Tag.Text}, item2 = new Item{name:title1.Text, Tag.Text}, item3 = new Item{name:title1.Text, Tag.Text}},
                   new Field{item2 = new Item{name:title1.Text, Tag.Text}, item2 = new Item{name:title1.Text, Tag.Text}, item3 = new Item{name:title1.Text, Tag.Text}},}
                   };


string jsonString = Items.ToJSON();
List Items=新列表{
新字段{item1=新项{name:title1.Text,Tag.Text},item2=新项{name:title1.Text,Tag.Text},item3=新项{name:title1.Text,Tag.Text},
新字段{item2=新项{name:title1.Text,Tag.Text},item2=新项{name:title1.Text,Tag.Text},item3=新项{name:title1.Text,Tag.Text},}
};
字符串jsonString=Items.ToJSON();
好的,很简单

您需要做的是首先创建一个名为field的对象,其中包含Item对象。然后Item对象有两个属性:tag和name。像这样的。但请注意,我没有测试代码,我只是在记事本上写的。但应该是这样的

导入此库:使用System.Web.Script.Serialization

然后创建这个类:

    public class Field
{
    public Field(){}
    public Item item1{set; get;}
    public Item item2{set; get;}
    public Item item3{set; get;}

}
还有一个叫做item的类

public class Item
    {
        public Item(){}
        public string name{set; get;}
        public string Tag{set; get;}

    }
然后像这样:

List<Field> Items = new List<Field>{
                   new Field{item1 = new Item{name:title1.Text, Tag.Text}, item2 = new Item{name:title1.Text, Tag.Text}, item3 = new Item{name:title1.Text, Tag.Text}},
                   new Field{item2 = new Item{name:title1.Text, Tag.Text}, item2 = new Item{name:title1.Text, Tag.Text}, item3 = new Item{name:title1.Text, Tag.Text}},}
                   };


string jsonString = Items.ToJSON();
List Items=新列表{
新字段{item1=新项{name:title1.Text,Tag.Text},item2=新项{name:title1.Text,Tag.Text},item3=新项{name:title1.Text,Tag.Text},
新字段{item2=新项{name:title1.Text,Tag.Text},item2=新项{name:title1.Text,Tag.Text},item3=新项{name:title1.Text,Tag.Text},}
};
字符串jsonString=Items.ToJSON();

这当然是可能的。最简单的方法是定义一个对应于JSON结构的类,创建它的实例并用字段中的数据填充它,然后序列化对象

类似地,您可以执行“从文件加载”功能。在这种情况下,加载JSON,将其反序列化为对象,然后用对象中的数据填充字段


考虑到序列化/反序列化过程本身,您可以在这里找到相当好的教程。

这当然是可能的。最简单的方法是定义一个对应于JSON结构的类,创建它的实例并用字段中的数据填充它,然后序列化对象

类似地,您可以执行“从文件加载”功能。在这种情况下,加载JSON,将其反序列化为对象,然后用对象中的数据填充字段


考虑到序列化/反序列化过程本身,您可以在这里找到非常好的教程。

您的代码在哪里?到目前为止你做了什么?我还没有对代码做任何事情,我只是创建了一个表单,就是这样,我在等待它是否可行。Json文件就在那里,向示例展示我希望它是什么样子。请参阅我的答案Joe,这是最好、最干净的方法。您可能需要对其进行一些调整,但它应该给您一个想法:)您的代码在哪里?到目前为止你做了什么?我还没有对代码做任何事情,我只是创建了一个表单,就是这样,我在等待它是否可行。Json文件就在那里,向示例展示我希望它是什么样子。请参阅我的答案Joe,这是最好、最干净的方法。您可能需要对其进行一些调整,但它应该会给您一个想法:)