如何批量编辑JSON个文件?
How to edit JSON files in batch?
所以我有一个我创建的 NFT 集合,它在同一个集合中包含 2 个不同的设计。
所以我想将它们合并在一起,但它们的 JSON 文件发生了冲突。
我要找的是:
- 增加特定字符串的特定数字。
- 全部删除一行。
看起来像这样:
{
"dna": "xxxxxxxxxxxxxxxxxxxx",
"name": "XXXXX XXXXX #0", ##I'd like to change this number value. I'd like to add 3000 on top of every other one.
"description": "xxxxxxxxxxxxxxxxxxxxxxxxxxx.",
"image": "REPLACE-THIS-WITH-YOUR-URL/0.png", #How to replace in batch?
"date": 1641544983764,
"attributes": [
{
"trait_type": "xxxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
}
],
"compiler": "xxxxxx" ##Delete this line from all
}
所以有 10.000 个这些文件,我不太可能手动编辑。
有什么建议吗?
基本代码是 (1) 获取所有 json 文件的列表 (2) 读取每个文件并解析为 json 对象 (3) 创建一个新对象并使所需的更改 (4) 保存新文件。作为旁注,我会在 运行 之前备份所有文件。这是基本代码,您可以与使用 C# 的人一起实现它。
private void button1_Click(object sender, EventArgs e)
{
//get all the json files
var jsonPath = @"c:\temp\json";
var jsonFiles = Directory.GetFiles(jsonPath, "*.json");
//loop through each file and process according to specs
foreach(var jsonFile in jsonFiles)
{
var json = File.ReadAllText(jsonFile);
var sample = JsonConvert.DeserializeObject<Sample>(json);
var sampleNew = new SampleNew();
sampleNew.dna = sample.dna;
sampleNew.name = sample.name + " 3000";
sampleNew.image = sample.image.Replace("oldurl", "newurl");
sampleNew.date = sample.date;
sampleNew.attributes = sample.attributes;
// serialize JSON to a string and then write string to a file
File.Delete(jsonFile);
File.WriteAllText(jsonFile, JsonConvert.SerializeObject(sampleNew));
}
}
public class Attribute
{
public string trait_type { get; set; }
public string value { get; set; }
}
public class Sample
{
public string dna { get; set; }
public string name { get; set; }
public string description { get; set; }
public string image { get; set; }
public long date { get; set; }
public List<Attribute> attributes { get; set; }
public string compiler { get; set; }
}
public class SampleNew
{
public string dna { get; set; }
public string name { get; set; }
public string description { get; set; }
public string image { get; set; }
public long date { get; set; }
public List<Attribute> attributes { get; set; }
}
是时候尝试一下,或者雇人了!
所以我有一个我创建的 NFT 集合,它在同一个集合中包含 2 个不同的设计。 所以我想将它们合并在一起,但它们的 JSON 文件发生了冲突。 我要找的是:
- 增加特定字符串的特定数字。
- 全部删除一行。
看起来像这样:
{
"dna": "xxxxxxxxxxxxxxxxxxxx",
"name": "XXXXX XXXXX #0", ##I'd like to change this number value. I'd like to add 3000 on top of every other one.
"description": "xxxxxxxxxxxxxxxxxxxxxxxxxxx.",
"image": "REPLACE-THIS-WITH-YOUR-URL/0.png", #How to replace in batch?
"date": 1641544983764,
"attributes": [
{
"trait_type": "xxxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
},
{
"trait_type": "xxx",
"value": "xxx"
}
],
"compiler": "xxxxxx" ##Delete this line from all
}
所以有 10.000 个这些文件,我不太可能手动编辑。 有什么建议吗?
基本代码是 (1) 获取所有 json 文件的列表 (2) 读取每个文件并解析为 json 对象 (3) 创建一个新对象并使所需的更改 (4) 保存新文件。作为旁注,我会在 运行 之前备份所有文件。这是基本代码,您可以与使用 C# 的人一起实现它。
private void button1_Click(object sender, EventArgs e)
{
//get all the json files
var jsonPath = @"c:\temp\json";
var jsonFiles = Directory.GetFiles(jsonPath, "*.json");
//loop through each file and process according to specs
foreach(var jsonFile in jsonFiles)
{
var json = File.ReadAllText(jsonFile);
var sample = JsonConvert.DeserializeObject<Sample>(json);
var sampleNew = new SampleNew();
sampleNew.dna = sample.dna;
sampleNew.name = sample.name + " 3000";
sampleNew.image = sample.image.Replace("oldurl", "newurl");
sampleNew.date = sample.date;
sampleNew.attributes = sample.attributes;
// serialize JSON to a string and then write string to a file
File.Delete(jsonFile);
File.WriteAllText(jsonFile, JsonConvert.SerializeObject(sampleNew));
}
}
public class Attribute
{
public string trait_type { get; set; }
public string value { get; set; }
}
public class Sample
{
public string dna { get; set; }
public string name { get; set; }
public string description { get; set; }
public string image { get; set; }
public long date { get; set; }
public List<Attribute> attributes { get; set; }
public string compiler { get; set; }
}
public class SampleNew
{
public string dna { get; set; }
public string name { get; set; }
public string description { get; set; }
public string image { get; set; }
public long date { get; set; }
public List<Attribute> attributes { get; set; }
}
是时候尝试一下,或者雇人了!