使用 datagridview 将列表序列化为现有的 .Json 文件

Posted

技术标签:

【中文标题】使用 datagridview 将列表序列化为现有的 .Json 文件【英文标题】:Serialize list to an existing .Json file, using a datagridview 【发布时间】:2021-12-29 12:25:03 【问题描述】:

我有一个反序列化的 json 文件直接输出到 DGV,并且在 DGV 中对最终用户可见。当他们完成使用软件并关闭时,我想将更新的DGV 序列化到我的 json 文件中。

当我反序列化这个json文件时,代码如下:

代码

    var v = JsonConvert.DeserializeObject<ToolJson> 
    (File.ReadAllText(@"\testLibrary.json"));

        //set DGV source to our datums 
        toolDataGridView.DataSource = v.Datums.Cast<IInteresting> 
        ().ToList();

当表单关闭时,我想将 DGV 读回我的 json 文件,格式与它已经完全相同。下面是 json 文件的 sn-p。我对 DGV 所做的只是跟踪数量、最后使用的员工以及是否需要退回工具。基本上是 json 文件的 5 个新条目。***属性。

Json 文件


"data": [
    
        "BMC": "carbide",
        "Cost": 0,
        "Employee": "",
        "GRADE": "Mill Generic",
        "Location": "",
        "description": "5/8-11\"",
        "geometry": 
            "CSP": false,
            "DC": 0.433,
            "HAND": true,
            "LB": 2,
            "LCF": 0.5,
            "NOF": 4,
            "NT": 1,
            "OAL": 5,
            "SFDM": 0.625,
            "TP": 0.0909091,
            "shoulder-length": 1.969,
            "thread-profile-angle": 60
        ,
        "guid": "0112c196-8a79-421d-8dda-d4aa964aa6d7",
        "holder": 
            "description": "Maritool CAT40-ER32-2.35",
            "guid": "e800051b-e2d6-4699-a2b6-dad6466a0a0c",
            "last_modified": 1485790626152,
            "product-id": "CAT40-ER32-2.35",
            "product-link": "",
            "segments": [
                
                    "height": 0.148,
                    "lower-diameter": 1.5,
                    "upper-diameter": 1.97
                ,
                
                    "height": 0.836,
                    "lower-diameter": 1.97,
                    "upper-diameter": 1.97
                ,
                
                    "height": 0.176,
                    "lower-diameter": 1.57,
                    "upper-diameter": 1.57
                ,
                
                    "height": 0.09,
                    "lower-diameter": 1.57,
                    "upper-diameter": 1.75
                ,
                
                    "height": 0.425,
                    "lower-diameter": 1.75,
                    "upper-diameter": 1.75
                ,
                
                    "height": 0.05,
                    "lower-diameter": 1.75,
                    "upper-diameter": 1.85
                ,
                
                    "height": 0.03,
                    "lower-diameter": 2.442,
                    "upper-diameter": 2.502
                ,
                
                    "height": 0.145,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.502
                ,
                
                    "height": 0.079,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.215
                ,
                
                    "height": 0.118,
                    "lower-diameter": 2.215,
                    "upper-diameter": 2.215
                ,
                
                    "height": 0.079,
                    "lower-diameter": 2.215,
                    "upper-diameter": 2.502
                ,
                
                    "height": 0.143,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.502
                ,
                
                    "height": 0.03,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.442
                ,
                
                    "height": 0.125,
                    "lower-diameter": 1.75,
                    "upper-diameter": 1.75
                
            ],
            "type": "holder",
            "unit": "inches",
            "vendor": "Maritool"
        ,
        "onHandQty": 0,
        "OrderQty": 5,
        "post-process": 
            "break-control": false,
            "comment": "",
            "diameter-offset": 17,
            "length-offset": 17,
            "live": true,
            "manual-tool-change": false,
            "number": 17,
            "turret": 0
        ,
        "product-id": "GMDTTM58-11UN4FL",
        "product-link": "6010",
        "start-values": 
            "presets": [
                
                    "description": "",
                    "f_n": 0.012242786571039,
                    "f_z": 0.0031,
                    "guid": "eac33213-4083-35b8-afe3-17bd42a249f0",
                    "n": 4410.76054758139,
                    "n_ramp": 44110,
                    "name": "1018",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 499.9999999999994,
                    "v_f": 54.6934307900093,
                    "v_f_leadIn": 54,
                    "v_f_leadOut": 54,
                    "v_f_plunge": 54,
                    "v_f_ramp": 54
                ,
                
                    "description": "",
                    "f_n": 0.010769117817118,
                    "f_z": 0.0028,
                    "guid": "f35f5601-68a9-477b-b4b0-2ffeedb3bef1",
                    "n": 1764.3042190325598,
                    "n_ramp": 1764,
                    "name": "4140",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 200,
                    "v_f": 19.7602072531647,
                    "v_f_leadIn": 19,
                    "v_f_leadOut": 19,
                    "v_f_plunge": 19,
                    "v_f_ramp": 19
                ,
                
                    "description": "",
                    "f_n": 0.012091641057817,
                    "f_z": 0.0031,
                    "guid": "b118ce46-da35-4ed6-9806-b98e05ffe077",
                    "n": 2646.45632854884,
                    "n_ramp": 2646,
                    "name": "Tool Steel",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 300,
                    "v_f": 32.8160584740056,
                    "v_f_leadIn": 32,
                    "v_f_leadOut": 32,
                    "v_f_plunge": 32,
                    "v_f_ramp": 32
                ,
                
                    "description": "",
                    "f_n": 0.01118476797848,
                    "f_z": 0.0028,
                    "guid": "0e1767f5-b0ef-422f-b49d-6cb8c3eb06ed",
                    "n": 3308.0704106860494,
                    "n_ramp": 3308,
                    "name": "Stainless Steel",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 375,
                    "v_f": 37.0503885996837,
                    "v_f_leadIn": 37,
                    "v_f_leadOut": 37,
                    "v_f_plunge": 37,
                    "v_f_ramp": 37
                
            ]
        ,
        "stockQty": 5,
        "type": "thread mill",
        "unit": "inches",
        "vendor": "Gorilla Mill"
    ,

【问题讨论】:

【参考方案1】:

清除随附的文本,我想我了解困难所在,确实它是我们需要使用的一种奇异数据类型,除非 json 中只有一个列表,在这种情况下,您可以简单地将表添加到命名数据集“工具" 并对其进行序列化,我已将两者都包含在内

public partial class frmWithGrid : Form
                 
    public frmWithGrid()
    
        InitializeComponent();

        string myDynamicJSON = "Imagine your testLibraryJson loaded here or later but before next line";
        DataTable convertedToTableToolData = ConvertToTableToolData(myDynamicJSON);

        dataGridView1.DataSource = convertedToTableToolData;
        //dataGridView1.DataMember = we're not storing it in a dataset so we can ignore this one
        dataGridView1.AutoGenerateColumns = true;   
        
        //TODO: Add bla bla to setup UI of cause, suppose you're able to navigate rows etc.
    

    DataTable ConvertToTableToolData(string jsonSource)
    
        //TODO: Insert own convertion code here, which seems to be in place
        return new DataTable(); // instead of this
    

    private void btnUserIsDone_Click(object sender, EventArgs e)
    
        //Obviously this shouldn't be directly in an event handler

        //Either you have many lists
        dynamic x = new ExpandoObject();
        var editedTable = (DataTable)dataGridView1.DataSource;
        //Will ignore that your OrderQty maybe wasn't originally part of data structure, because it may have been
        //and it doesn't change anything with the principle, and adds needless complexity
        var propertiesDictionary = (IDictionary<string, object>)x;
        propertiesDictionary.Add("Tool", editedTable);

        //TODO: Add other sets to json if applicable

        string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(x);



        //or just one
        var ds = new DataSet("Tool");
        ds.Tables.Add(editedTable.Clone());
        serialized = Newtonsoft.Json.JsonConvert.SerializeObject(ds);
    

【讨论】:

微软文档绑定到 SQL。这对我来说很不清楚。我怎样才能将我的数据网格表绑定到现有的数据表,然后对其进行序列化? @JoeFisher 你决定使用数据表了吗?我建议跳过它,因为这是需要很好工作的手动部分,但会更新如何使用数据表和绑定源示例 显然你可以同时序列化tooltaSet和tooldataSet.Tables["Tool"] 我对数据网格视图进行了更改。 DGV 中的数据是否直接关联到我的数据表?我预计我需要将数据从 gridview 发送回数据表,然后序列化。也和那条序列化线一样简单吗?我不需要一个 foreach 循环来先建立一个列表? 是的,它可以,因为您必须设置自动生成列或定义相关列,以防数据始终不清楚是哪种类型。在某些情况下,当您想要支持编辑、插入等时,拥有一个实际的绑定源可能是可行的。 Newtonsoft.Json 将表行序列化为一个元素数组,这与序列化的元素列表完全相同,所以我怀疑您不必做任何特别的事情。如果您反序列化,您可能必须根据可空字段来具体说明,这样会猜测字符串,而这往往不是正确的

以上是关于使用 datagridview 将列表序列化为现有的 .Json 文件的主要内容,如果未能解决你的问题,请参考以下文章

将 JSON 反序列化为现有对象 (Java)

升级到 Rails 5 时,序列化为哈希的现有数据会产生错误

Jackson - 将内部对象列表反序列化为更高级别的列表

将列表序列化为 JSON

如何将列表序列化为字符数组

C ++使用for循环将对象列表序列化为XML