简体   繁体   中英

Serializing class to .json file

just trying to do a simple serialize(first time trying). actually had this working up until I changed a few things and added a deserialize and added a class that made my other way no longer work.

basically I took what I had for deserialize json to object and tried to just reverse the order of things. but now I get an error at a foreach loop I'm not sure if I even need. Once I get the serialize working I'm sure I will also be stuck on how to format the string as it enters the .json file so it appends properly but that is for another day.

here is error i received

System.NullReferenceException: 'Object reference not set to an instance of an object.'

i receive this exception on the line foreach(var translogs in Logs.transLogs)

here is my event.

Code

private void toolPull_Click(object sender, EventArgs e)
{
    double cost = Convert.ToDouble(active_Cost.Text);
    int serial = Convert.ToInt32(transactionSerial_Box.Text);
    DateTime timeNow = DateTime.Now;
    TransactionLogs Logs = new TransactionLogs();
    foreach(var translogs in Logs.transLogs)
    {
        translogs.Employee = transactionEmployee_Box.Text;
        translogs.Serial = serial;
        translogs.Cost = cost;
        translogs.Description = active_Description.Text;
        translogs.CurrentDate = timeNow;
    }

    string stringJson = JsonConvert.SerializeObject(Logs);
    StreamWriter sw = new StreamWriter(@"C:\transactionlog.json", append: true);
    sw.WriteLine(stringJson);
    sw.Close();
}

Here is the class to work with json

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class TransactionLogs
    {
        [JsonProperty("TransactionLog")]
        public List<TransactionLog> transLogs { get; set; }
    }

    public partial class TransactionLog
    {
        [JsonProperty("employee")]
        public string Employee { get; set; }

        [JsonProperty("currentDate")]
        public DateTime CurrentDate { get; set; }

        [JsonProperty("serial")]
        public int Serial { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("isPull")]
        public bool IsPull { get; set; }

        [JsonProperty("cost")]
        public double Cost { get; set; }
    }
}

and here is json file

{
   "TransactionLog":[
      {
         "employee":"Joey",
         "currentDate":"2021-11-03T11:49:13.5741628-04:00",
         "serial":1111,
         "description":"1/2-20 Threadmill",
         "isPull":true,
         "_ost":25.68
      },
      {
         "employee":"joey",
         "currentDate":"2021-11-03T11:50:34.6344474-04:00",
         "serial":1000,
         "description":"1/2-20 Threadmill",
         "isPull":true,
         "cost":25.68
      },
      {
         "employee":"john",
         "currentDate":"2021-11-03T11:50:40.9956616-04:00",
         "serial":2000,
         "description":"1/2-20 Threadmill",
         "isPull":true,
         "cost":25.68
      },
      {
         "employee":"Jim",
         "currentDate":"2021-11-03T11:51:24.5559292-04:00",
         "serial":4565,
         "description":"1/2-20 Threadmill",
         "isPull":true,
         "cost":25.68
      }
   ]
}

Let me capture here the outcome of the comments.

There were two problems with these two lines:

TransactionLogs Logs = new TransactionLogs();
foreach(var translogs in Logs.transLogs)
  • The TransactionLogs 's transLogs collection is not initialized, that's caused the NRE
  • After fixing that the foreach went through on an empty collection

The fix for the first problem:

Logs.transLogs = new List<TransactionLog>();

The fix for the second problem:

var transLogs = new TransactionLog()
{
    Employee = transactionEmployee_Box.Text;
    Serial = serial;
    Cost = cost;
    Description = active_Description.Text;
    CurrentDate = timeNow;
};
Logs.transLogs.Add(transLogs);

So, rather than iterating through the empty collection, you had to populate it by adding a new member.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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