简体   繁体   中英

C# JSON.NET remove last part from file

I am using C# 2015, and a form with a datagrdview in it. My user enters some data and clicks a button. Then with this code it generates some json:

        var llist = new List<Nachrichten_Felder>();
        //Loop through datagridview rows
        foreach (DataGridViewRow row in dgvNachrichten.Rows)
        {
            string datum = null;
            string nachricht = null;
            if (row.Cells["Datum"].Value != null)
                datum = row.Cells["Datum"].Value.ToString();
            if (row.Cells["Nachricht"].Value != null)
                nachricht = row.Cells["Nachricht"].Value.ToString();
            var obj = new Nachrichten_Felder()
            {
                Datum = datum,
                Nachricht = nachricht
            };
            llist.Add(obj);
        }

My datagridview gives me after exporting the follwing JSON:

{
      "export": [
        {
          "value": "de",
          "tp": "df"
        },
        {
          "value": "rr",
          "tp": "df"
        },
        {
          "value": null,
          "tp": null
        }
      ]
    }

How can I use JSON.net to remove the last part with "null"? I guess these null values are coming from the last datagridview row which is emtpy...

Like this?

 //Setup list object
                var llist = new List<Nachrichten_Felder>();
                //Loop through datagridview rows
                foreach (DataGridViewRow row in dgvNachrichten.Rows)
                {
                    string datum = null;
                    string nachricht = null;
                    if ((row.Cells["Datum"].Value != null) && (row.Cells["Nachricht"].Value != null))
                        if (row.Cells["Datum"].Value != null)
                        datum = row.Cells["Datum"].Value.ToString();
                    if (row.Cells["Nachricht"].Value != null)
                        nachricht = row.Cells["Nachricht"].Value.ToString();
                    var obj = new Nachrichten_Felder()
                    {
                        Datum = datum,
                        Nachricht = nachricht
                    };
                    llist.Add(obj);
                }

Did you notice when you do:

var llist = new List<Nachrichten_Felder>();
    //Loop through datagridview rows
    foreach (DataGridViewRow row in dgvNachrichten.Rows)
    {
        string datum = null;
        string nachricht = null;
        if (row.Cells["Datum"].Value != null)
            datum = row.Cells["Datum"].Value.ToString();
        if (row.Cells["Nachricht"].Value != null)
            nachricht = row.Cells["Nachricht"].Value.ToString();
        var obj = new Nachrichten_Felder()
        {
            Datum = datum,
            Nachricht = nachricht
        };
        llist.Add(obj);
    }

obj is being instantiated even though you don't have any data on datum or nachricht?

Before trying to remove the JSON, try to change your logic for

if ((row.Cells["Datum"].Value != null) && (row.Cells["Nachricht"].Value != null)) { ... }

You can do it in two ways to be honest.

Approach 1: Don't add anything null when you're populating the list.

var llist = new List<Nachrichten_Felder>();
//Loop through datagridview rows
foreach (DataGridViewRow row in dgvNachrichten.Rows)
{
    string datum = null;
    string nachricht = null;
    datum = row.Cells["Datum"].Value?.ToString();
    nachricht = row.Cells["Nachricht"].Value?.ToString();
    if (datum != null && nachricht! = null)
    {
        var obj = new Nachrichten_Felder()
        {
            Datum = datum,
            Nachricht = nachricht
        };
        llist.Add(obj);
   }
}

Approach 2: Use Linq

Your whole implementation would be drastically simpler if you use LINQ

    var list = dgvNachrichten.Rows.Cast<DataGridViewRow>()
        .Where(x => x.Cells["Datum"].Value != null && x.Cells["Nachricht"].Value != null)
        .Select(x => new Nachrichten_Felder()
        {
            Datum = x.Cells["Datum"].Value.ToString(),
            Nachricht = x.Cells["Datum"].Value.ToString()
        }).ToList();

Populating the rest of the answer shortly and refactoring approach 1.

You must set config for Jsn.Net and ignore Null Value like this :

JsonConvert.SerializeObject(myObject, 
                        Newtonsoft.Json.Formatting.None, 
                        new JsonSerializerSettings { 
                            NullValueHandling = NullValueHandling.Ignore
                        });

Or

JsonSerializer _jsonWriter = new JsonSerializer {
                             NullValueHandling = NullValueHandling.Ignore
                         };

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