[英]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.