如何生成新的JSON文件,其中可能包含旧JSON文件中的一个引号



我写了一些代码,这些代码必须读取5000个JSON文件,并替换每个文件中出现两次的数字。它被设置为创建一个新的json文件并删除旧文件,这样,其他数据也必须传输到新文件中,这不是问题,但我们有一个名为attributes的列表,其中包含一些使用单引号(用于标点符号(的文本-然而,当写入新json文件时,带有单引号的文本会发生更改,单引号将替换为u0027

示例:旧文件包含:"0.png""Hen's Secret Bra",在新文件中更改为"5000.png"正确,"Henu0027s Secret Bra"不正确。如何正确转移单个报价?

这是我的代码:

using System.Text.Json;
using System.Web;
internal class Program
{
private static void Main(string[] args)

{
//get all the json files
var jsonPath = @"/Users/jasonnienaber/Desktop/nft/cef/official/fatJSONselfFIX/test";
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 = JsonSerializer.Deserialize<Sample>(json);
var sampleNew = new SampleNew();
var intCounter = 0;
var newIntCounter = intCounter+5000;
var Counter = intCounter.ToString();
var newCounter = newIntCounter.ToString();
sampleNew.image = sample.image.Replace(Counter, newCounter);
sampleNew.name = sample.name.Replace(Counter, newCounter);
sampleNew.description = sample.description;
sampleNew.external_url = sample.external_url;
sampleNew.attributes = sample.attributes;
// serialize JSON to a string and then write string to a file
File.Delete(jsonFile);
File.WriteAllText(jsonFile, JsonSerializer.Serialize(sampleNew));
intCounter++;

}

}    
public class Attribute
{
public string trait_type { get; set; }
public string value { get; set; }

}

public class Sample
{
// public string dna { get; set; }
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; }
}

public class SampleNew
{
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 List<Attribute> attributes {get; set;}
}
}

"Hen's Secret Bra"在属性内

如Jon Skeet所述,使用u0027是有效的,但是,您可以更改序列化程序编码选项:

using System.Text.Json;
var obj = new
{
Name = "John's awesome object",
Age = 31
};
var options = new JsonSerializerOptions
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
// writes obj to json file
File.WriteAllText("obj.json", JsonSerializer.Serialize(obj, options));

输出

{"Name":"John's awesome object","Age":31}

因此,在您的代码中,您可以执行以下操作:

var options = new JsonSerializerOptions
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
//loop through each file and process according to specs
foreach (var jsonFile in jsonFiles)
{
var json = File.ReadAllText(jsonFile);
var sample = JsonSerializer.Deserialize<Sample>(json);
var sampleNew = new SampleNew();
var intCounter = 0;
var newIntCounter = intCounter + 5000;
var Counter = intCounter.ToString();
var newCounter = newIntCounter.ToString();
sampleNew.image = sample.image.Replace(Counter, newCounter);
sampleNew.name = sample.name.Replace(Counter, newCounter);
sampleNew.description = sample.description;
sampleNew.external_url = sample.external_url;
sampleNew.attributes = sample.attributes;
// serialize JSON to a string and then write string to a file
File.Delete(jsonFile);
// use serializer options
File.WriteAllText(jsonFile, JsonSerializer.Serialize(sampleNew, options));
intCounter++;
}

请参阅UnsafeRelaxedJson逃离

最好使用Newtonsoft。Json,以避免任何像这样的问题。

但是,您可以尝试解决问题的一件事是使用序列化选项

var options = new JsonSerializerOptions
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};

但是是不安全的

另一种选择更尴尬但更安全,因为它只影响一个符号,而不是第一种情况下的

var attr= new Attribute{trait_type="trait_type", value="Hen's Secret Bra"};    
attr.value=attr.value.Replace("'","==="); //or you can try something else if you don't like ===
var json=System.Text.Json.JsonSerializer.Serialize(attr);
json=json.Replace("===","'");

File.WriteAllText(jsonFile, json);

json = File.ReadAllText(jsonFile);

结果

{"trait_type":"trait_type","value":"Hen's Secret Bra"}

最新更新