C# 在将 JSON 数据写入文件时不删除反斜杠
C# Doesn't Remove Backslashes While Writing JSON Data Into File
我正在尝试将一些数据添加到一个大的 json 文件中,所以我尝试在 C# 中执行此操作,我已经打开该文件,将我的数据写入其中,但是在将最终数据写入其中时.Json文件,JsonConverter.SerializeObject returns的字符串有反斜杠,原来的字符串没有(Text Visualizer
仔细看没有出现,但是最后的数据写入 .Json 文件的那个仍然有反斜杠。
这就是我查看 Text Visualizer 时的样子;
{
"GID_0": "TUR",
"NAME_0": "Turkey",
"GID_1": "TUR.1_1",
"NAME_1": "Adana",
"NL_NAME_1": "",
"GID_2": "TUR.1.1_1",
"NAME_2": "Aladağ",
"VARNAME_2": "",
"NL_NAME_2": "",
"TYPE_2": "District",
"ENGTYPE_2": "District",
"CC_2": "",
"HASC_2": "TR.AA.AL",
"NUFUS": "16653"
}
但是文件中的真实数据是这样的;
"{\r\n \"GID_0\": \"TUR\",\r\n \"NAME_0\": \"Turkey\",\r\n \"GID_1\": \"TUR.1_1\",\r\n \"NAME_1\": \"Adana\",\r\n \"NL_NAME_1\": \"\",\r\n \"GID_2\": \"TUR.1.10_1\",\r\n \"NAME_2\": \"Aladağ\",\r\n \"VARNAME_2\": \"\",\r\n \"NL_NAME_2\": \"\",\r\n \"TYPE_2\": \"District\",\r\n \"ENGTYPE_2\": \"District\",\r\n \"CC_2\": \"\",\r\n \"HASC_2\": \"TR.AA.AS\",\r\n \"NUFUS\": \"16653\"\r\n}"
这就是我尝试在代码中执行此操作的方式;
using (StreamReader r = new StreamReader(@"D:\districts_of_turkey.json"))
{
string json = r.ReadToEnd();
JObject results = JObject.Parse(json);
foreach(var result in results["features"])
{
string type = (string)result["type"];
string geometryType = (string)result["geometry"]["type"];
JArray geometryStr = JArray.FromObject(result["geometry"]["coordinates"]);
string properties = result["properties"].ToString();
var propertiesArray = JsonConvert.DeserializeObject<PropertiesForJSON>(properties);
for (int j = 0; j < districts.Count - 1; j++)
{
string district = districts[j].Ilce.Split('-')[0].Split('(')[1].TrimEnd(')').ToUpper(turkey);
string province = districts[j].Ilce.Split('-')[0].Split('(')[0].ToUpper(turkey);
if ((province == propertiesArray.NAME_1.ToUpper(turkey) || province == propertiesArray.NAME_1) && (district == propertiesArray.NAME_2.ToUpper(turkey) || district == propertiesArray.NAME_2))
{
propertiesArray.NUFUS = districts[j].Nufus;
lst.Add(propertiesArray);
break;
}else if(j == districts.Count - 2)
{
exceptions.Add("İL = " + propertiesArray.NAME_1 + " // İLÇE = " + propertiesArray.NAME_2);
}
}
/*
{"GID_0":"TUR","NAME_0":"Turkey","GID_1":"TUR.32_1","NAME_1":"Eskişehir","NL_NAME_1":"","GID_2":"TUR.32.10_1","NAME_2":"Mihalıççık","VARNAME_2":"","NL_NAME_2":"","TYPE_2":"District","ENGTYPE_2":"District","CC_2":"","HASC_2":"TR.ES.MK"}
*/
string propertyStr = JsonConvert.SerializeObject(propertiesArray);
propertyStr = removeBackSlash(JToken.Parse(propertyStr).ToString());
string propertyStr = JsonConvert.SerializeObject(propertiesArray);
result["properties"] = propertyStr;
}
File.WriteAllText(@"D:\districts_with_populationtwo.json", results.ToString());
}
public class PropertiesForJSON
{
public string GID_0;
public string NAME_0;
public string GID_1;
public string NAME_1;
public string NL_NAME_1;
public string GID_2;
public string NAME_2;
public string VARNAME_2;
public string NL_NAME_2;
public string TYPE_2;
public string ENGTYPE_2;
public string CC_2;
public string HASC_2;
public string NUFUS;
}
这也是我将最终数据写入文件的方式(上面的代码是一个结果);
File.WriteAllText(@"D:\districts_with_population.json", results.ToString());
我怎样才能真正将字符串写入 JSON 格式的文件中?
正如评论中指出的那样,问题在于您在此处将对象转换为 JSON 字符串:
string propertyStr = JsonConvert.SerializeObject(propertiesArray);
然后在此处将该字符串(包括双引号等)设置为 JSON 属性:
result["properties"] = propertyStr;
我相信您不希望它作为字符串属性,而只是作为对象。因此,您希望 属性 的值成为 JToken
本身。所以我希望它能起作用:
result["properties"] = JToken.FromObject(propertiesArray);
我正在尝试将一些数据添加到一个大的 json 文件中,所以我尝试在 C# 中执行此操作,我已经打开该文件,将我的数据写入其中,但是在将最终数据写入其中时.Json文件,JsonConverter.SerializeObject returns的字符串有反斜杠,原来的字符串没有(Text Visualizer
仔细看没有出现,但是最后的数据写入 .Json 文件的那个仍然有反斜杠。
这就是我查看 Text Visualizer 时的样子;
{
"GID_0": "TUR",
"NAME_0": "Turkey",
"GID_1": "TUR.1_1",
"NAME_1": "Adana",
"NL_NAME_1": "",
"GID_2": "TUR.1.1_1",
"NAME_2": "Aladağ",
"VARNAME_2": "",
"NL_NAME_2": "",
"TYPE_2": "District",
"ENGTYPE_2": "District",
"CC_2": "",
"HASC_2": "TR.AA.AL",
"NUFUS": "16653"
}
但是文件中的真实数据是这样的;
"{\r\n \"GID_0\": \"TUR\",\r\n \"NAME_0\": \"Turkey\",\r\n \"GID_1\": \"TUR.1_1\",\r\n \"NAME_1\": \"Adana\",\r\n \"NL_NAME_1\": \"\",\r\n \"GID_2\": \"TUR.1.10_1\",\r\n \"NAME_2\": \"Aladağ\",\r\n \"VARNAME_2\": \"\",\r\n \"NL_NAME_2\": \"\",\r\n \"TYPE_2\": \"District\",\r\n \"ENGTYPE_2\": \"District\",\r\n \"CC_2\": \"\",\r\n \"HASC_2\": \"TR.AA.AS\",\r\n \"NUFUS\": \"16653\"\r\n}"
这就是我尝试在代码中执行此操作的方式;
using (StreamReader r = new StreamReader(@"D:\districts_of_turkey.json"))
{
string json = r.ReadToEnd();
JObject results = JObject.Parse(json);
foreach(var result in results["features"])
{
string type = (string)result["type"];
string geometryType = (string)result["geometry"]["type"];
JArray geometryStr = JArray.FromObject(result["geometry"]["coordinates"]);
string properties = result["properties"].ToString();
var propertiesArray = JsonConvert.DeserializeObject<PropertiesForJSON>(properties);
for (int j = 0; j < districts.Count - 1; j++)
{
string district = districts[j].Ilce.Split('-')[0].Split('(')[1].TrimEnd(')').ToUpper(turkey);
string province = districts[j].Ilce.Split('-')[0].Split('(')[0].ToUpper(turkey);
if ((province == propertiesArray.NAME_1.ToUpper(turkey) || province == propertiesArray.NAME_1) && (district == propertiesArray.NAME_2.ToUpper(turkey) || district == propertiesArray.NAME_2))
{
propertiesArray.NUFUS = districts[j].Nufus;
lst.Add(propertiesArray);
break;
}else if(j == districts.Count - 2)
{
exceptions.Add("İL = " + propertiesArray.NAME_1 + " // İLÇE = " + propertiesArray.NAME_2);
}
}
/*
{"GID_0":"TUR","NAME_0":"Turkey","GID_1":"TUR.32_1","NAME_1":"Eskişehir","NL_NAME_1":"","GID_2":"TUR.32.10_1","NAME_2":"Mihalıççık","VARNAME_2":"","NL_NAME_2":"","TYPE_2":"District","ENGTYPE_2":"District","CC_2":"","HASC_2":"TR.ES.MK"}
*/
string propertyStr = JsonConvert.SerializeObject(propertiesArray);
propertyStr = removeBackSlash(JToken.Parse(propertyStr).ToString());
string propertyStr = JsonConvert.SerializeObject(propertiesArray);
result["properties"] = propertyStr;
}
File.WriteAllText(@"D:\districts_with_populationtwo.json", results.ToString());
}
public class PropertiesForJSON
{
public string GID_0;
public string NAME_0;
public string GID_1;
public string NAME_1;
public string NL_NAME_1;
public string GID_2;
public string NAME_2;
public string VARNAME_2;
public string NL_NAME_2;
public string TYPE_2;
public string ENGTYPE_2;
public string CC_2;
public string HASC_2;
public string NUFUS;
}
这也是我将最终数据写入文件的方式(上面的代码是一个结果);
File.WriteAllText(@"D:\districts_with_population.json", results.ToString());
我怎样才能真正将字符串写入 JSON 格式的文件中?
正如评论中指出的那样,问题在于您在此处将对象转换为 JSON 字符串:
string propertyStr = JsonConvert.SerializeObject(propertiesArray);
然后在此处将该字符串(包括双引号等)设置为 JSON 属性:
result["properties"] = propertyStr;
我相信您不希望它作为字符串属性,而只是作为对象。因此,您希望 属性 的值成为 JToken
本身。所以我希望它能起作用:
result["properties"] = JToken.FromObject(propertiesArray);