简体   繁体   English

如何删除包含 C# 中特定文本的 JSON 文件的多个部分

[英]How do I delete multiple portions of a JSON file that contain specific text in C#

I have 10,000 JSON files which contain attributes, some of which are marked as Empty .我有 10,000 个包含属性的 JSON 文件,其中一些被标记为Empty

Attributes that must stay look like this: {"trait_type":"Skin","value":"Euphoria"}, whilst attributes that must be removed look like this {"trait_type":"Tattoo","value":"Empty"},必须保留的属性如下所示: {"trait_type":"Skin","value":"Euphoria"},而必须删除的属性如下所示{"trait_type":"Tattoo","value":"Empty"},

These empty attributes appear multiple times in some JSON files, while sometimes none in others.这些空属性在某些 JSON 文件中出现多次,而有时在其他文件中则没有。

Here is my code, however I must apologise is it seems that i've barely tried, but I've deleted most of it now out of frustration.这是我的代码,但是我必须道歉,我似乎几乎没有尝试过,但是出于沮丧我现在已经删除了大部分。

using System.Web;
using System.Text.Encodings.Web;

internal class Program
{
    private static void Main(string[] args)
   
{
     var jsonPath = @"/Users/jasonnienaber/Desktop/nft/cef/official/fatJSONselfFIX/test/test2";
     var jsonFiles = Directory.GetFiles(jsonPath, "*.json").OrderBy(f => f);

     string toRemove = "Empty";

     foreach(var jsonFile in jsonFiles)
     {
        var json = File.ReadAllText(jsonFile);
        var sample = JsonSerializer.Deserialize<Sample>(json);
    
        if (json.Contains(toRemove))
        {

        }
     }
}
 public class Attribute
        {
            public string trait_type { get; set; } 
            public string value { get; set; }
    
        }
    
        public class Sample
        {
            public string image { get; set; }
            public string name { get; set; }
            public string description { get; set; }
            public string external_url { get; set; }
            public List<Attribute> attributes { get; set; }
            public string compiler { get; set; }
        }

}

I'm also aware that I have not re-sereliazed yet, and i'll need to delete the old file and write to a new file.我也知道我还没有重新序列化,我需要删除旧文件并写入一个新文件。 I'm new to C# and just need assistance deleting the aforementioned Empty attributes.我是 C# 的新手,只需要帮助删除上述属性。

I did it with .Net 6 and install Newtonsoft package.我用.Net 6完成了它并安装了Newtonsoft package。 I did only the part that removes the wanted records from JSON.我只做了从 JSON 中删除所需记录的部分。 From here you can go and iterate the files and do your updates.从这里您可以 go 并迭代文件并进行更新。

using Newtonsoft.Json.Linq;

internal partial class Program
{

static void Main(string[] args)
{
    string json = @"{""image"":""img"",
                    ""name"":""name"",
                    ""description"":""description"",
                    ""external_url"":""external_url"",
                    ""attributes"":[
                                    {""trait_type"":""Skin"",""value"":""Euphoria""},
                                    { ""trait_type"":""Tattoo"",""value"":""Empty""}
                                    ],
                    ""compiler"":""compiler""
                    }";
    
    dynamic jObj = JObject.Parse(json);
    
    var lst = (jObj.attributes as IEnumerable<object>).ToList();
        lst.RemoveAll(x => (x as dynamic).value == "Empty");

    dynamic outJosn = new
    {
        image = jObj.image,
        name = jObj.name,
        description = jObj.description,
        external_url= jObj.external_url,
        attributes = (dynamic)lst,
        compiler = jObj.compiler
    };
    
    string output = Newtonsoft.Json.JsonConvert.SerializeObject(outJosn);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果文本文件中的一行与我的组合框中的字符串匹配,我该如何删除或修改 C# - How do I delete or modify a line in a text file if it matches the string in my combo box C# 如何删除巨大PDF中的页面顶部没有特定单词的页面? 希望在C#中 - How can I delete the pages in an enormous PDF that do not contain a certain word at the top of the page? Hopefully in C# c# 如何防止将一个特定字符添加到文本文件中 - c# how do i prevent one specific character from being added to a text file 如何删除C#中xmlreader打开的文件? - How do I delete a file that was opened by xmlreader in c#? 如何使用 C# 在 S3 中删除文件? - How do I delete a file in S3 with C#? 我如何在包含C#中的列表的对象列表中的特定字段上询问if参数? - how do I ask an if argument on a specific field which in a list of objects that contain a list in c#? 在 C# 中,如何处理具有多个线程/任务但有条件的大型文本文件? - In C#, how do I process a large text file with multiple threads/tasks, but with conditions? C#删除第i行文本文件(如果它与特定术语不同) - C# Delete line i text file if it is different than specific term 如何在C#中的特定XML文件中读取Element? - How do I read Element in specific XML file in C#? C# 如何将字符串与文件中的特定行进行比较? - C# How do I compare a string to a specific line in a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM